java - When would you use a WeakHashMap or a WeakReference? -
the use of weak references i've never seen implementation of i'm trying figure out use case them , how implementation work. when have needed use weakhashmap
or weakreference
, how used?
one problem strong references caching, particular large structures images. suppose have application has work user-supplied images, web site design tool work on. naturally want cache these images, because loading them disk expensive , want avoid possibility of having 2 copies of (potentially gigantic) image in memory @ once.
because image cache supposed prevent reloading images when don't absolutely need to, realize cache should contain reference image in memory. ordinary strong references, though, reference force image remain in memory, requires somehow determine when image no longer needed in memory , remove cache, becomes eligible garbage collection. forced duplicate behavior of garbage collector , manually determine whether or not object should in memory.
understanding weak references, ethan nicholas
Comments
Post a Comment