Picasso clear memory [duplicate]

允我心安 提交于 2019-12-08 06:46:10

问题


I have many images loaded using Picasso in a view flipper which is in a fragment. I want to know if the images are still in memory when I change to a different fragment. If they persist in the memory how can I remove them.

Any suggestions on any other method to load images to a view flipper would be appreciated. The images are in the internal memory of the device.

Thanks in advance.


回答1:


Try.. setIndicatorsEnabled(true). It refers to where your loaded image from. Also, check the official document about DEBUG INDICATORS within the link. enter link description here

To avoid Picasso loading image from cache.

Please set

.memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE) .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE)

with your needs.




回答2:


I had (have?) a problem like that. In my case it was master - detail pattern with "gallery" inside of ViewPager and single image fragment. Picasso it's self uses LRU cache for 1/7th of available memory and I don't advise to change it - for 99% it will only postpone the OOM caused crash for a moment. In my case references to images were kept by other objects causing leaks.

In my case what helped: I played with the app for a moment to make it grow in memory and then, using Android Device Monitor I created hprof dump. This file needs to be converted with hprof-conv from sdk (platform-tools directory) Translated file can be viewed in Eclipse Memory Analyzer Tool (it's available also as standalone application as well as Eclipse plugin). Unfortunately, looking for big objects was not helpful - as one may expect - there is a lot of bitmaps in application made for viewing images. I used histogram tool to find objects that exist in a large numbers and it was much more helpful approach especially after filtering results by my own application package. Finally I've found solution for my own problems and actions taken was: I made "details" fragment single instance (a little hack with factory method) - for unknown reasons previous instances of this class were kept in memory and not collected by GC. I changed ViewPager to RecyclerView, as ViewPager kept too many spare views in memory with it's data off course. I also made some improvements with connectivity, to make it less frequent giving GC chance to work better.

What I also recomend is:

Use .fit() or .resize() methods of Picasso to limit size of images put into image views. If possible create images sized for your needs (needs backend development). Checking frequently for wasted memory, to make those investigations easier.



来源:https://stackoverflow.com/questions/30794891/picasso-clear-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!