Reduce the impact of videos in memory to prevent crashes

不羁的心 提交于 2021-02-19 09:04:33

问题


I have an app with potentially a very large amount of images and videos, depending on how many posts users make.

Already in development, with relatively few posts containing tabs with images and videos, occasionally the app crashes on real devices, I fear due to too much memory usage.

Are there any tips and strategies to optimize resource usage?

As for images, I only use medium-resolution regular EncodedImages (no multi-images), as backgrounds in Labels of fixed size.

As for videos, I only use short videos.

As for posts and videos (which are inside complex multilayer hierarchies with many Tabs), I use my own custom version of InfiniteContainer that, unlike the "default" version, never uses "removeAll" during a refresh, but just adds new posts, if any: this has already given me a great benefit in terms of performance. I also try to reuse the same instance of an InfiniteContainer full of images and videos, so I don't have to recreate every time the Media instances: also in this case there is a performance advantage, but everything remains loaded in memory.

Does the garbage collector do its job with videos?

It's not clear to me if Media.cleanup() does something or not with videos.

Thanks for the suggestions.


回答1:


I suggest using the Android memory profiler to try and track potential memory leaks and the source of such leaks. You can also use memory tracking on the simulator but that's a bit more challenging as some overhead stored in the "native system" would be hidden from Java based profilers.

EncodedImage is great for images that aren't shown. When they're shown they are "locked" and take up a lot of RAM. At that point they can't get GC'd. Make sure your infinite container unlocks components that are no longer a part of it. If you keep adding things to the infinite container you'll eventually run out of RAM. There's nothing you can do about that.

Images get "locked" when they're in use (e.g. in the current form but not necessarily visible). It's the responsibility of the form to unlock them otherwise the image won't be GCd at all. You can use isLocked() to determine if the encoded image takes up the full amount of RAM. Notice that unlocked images might perform very poorly as they'll thrash the GC so tread lightly with this...

I would suggest using FileEncodedImageAsync for data so files can be completely removed from RAM and using it for videos too (using a thumbnail. I suggest placing the video itself only when playback starts.

The difference from EncodedImage is this: Even when it is GCd the EncodedImage keeps the "encoded" part (PNG, JPEG data). FileEncodedImageAsync has the data in FileSystem and as such the cache can be managed as files. Furthermore, the image only takes up the name of the image file in RAM assuming it isn't locked.



来源:https://stackoverflow.com/questions/66199776/reduce-the-impact-of-videos-in-memory-to-prevent-crashes

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