jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Weak References

We have been discussing memory stuff for a while now. One last topic (for now) when working with memory are weak references.  

You know how if a item in the memory is 'live' (being referenced by a root item) it will never be cleared by the garbage collector. This is good because you don’t want to lose reference to something that is currently being used. However, what if you are working with large list that your project is using? Large lists (that are still referenced) will take up a lot of memory. Keeping them in the memory may be good in theory but the performance hit (memory usage) might not be worth it.  

Perhaps you don’t mind if these objects are cleared from memory everytime a garbage collection happens. If they are lost, its okay. You can just run another query from where you can simply build a new list and have it populated. You could simply check if it is still in memory before using it. If it is, you use it from memory. If it is not in memory, you simply load the stuff from the server or a file or from wherever the list was originally populated. 

The use case for this is pretty straightforward. If your application frequently uses lists that become populated with lots of data (and the list itself gets very long) it may become easier – performance wise - to simply reload the list rather than keep it in memory.  

Since I have not yet started blogging about lists, I am just going to leave the code out of this post for now. 


Follow me on twitter, facebook and instagram for more updates. Thanks!

Comments are closed