Java – softreference collects garbage too early

I am implementing a caching mechanism for my Android application@ H_ 404_ 2 @ I use softreference, just like many examples I found The problem is that when I scroll up or down in the listview, most of the images have been cleared I can see in logcat that every time the application loads a new image, my application is garbage collected This means that most invisible images in the listview will disappear

Solution

Softreference is a poor man cache The JVM can keep these references longer, but it does not have to Once there are no hard references, the JVM can garbage collect soft - referenced objects The behavior of the JVM you encounter is correct because the JVM does not need to persist such objects Of course, most JVMs try to maintain the existence of soft reference objects to some extent@ H_ 404_ 2 @ so softreferences is a dangerous cache If you really want to ensure caching behavior, you need a real cache Like an LRU cache Especially if your cache is critical to performance, you should use the appropriate cache

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>