且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

安卓:内存不足错误

更新时间:2022-06-27 00:08:15

嘛。 Android上的位图可以是一个有点棘手。你可以给有关的位图和它们的大小来源更好的信息?

Well. Bitmaps on Android can be a little tricky. Can you give better information about the sources of the bitmaps and their sizes?

另外,我建议你寻找到它上了几级:

Otherwise, I'd recommend looking into it on a few levels:

1。查看 inPurgeable 标志。它也被推荐的 Facebook的工程博客

1. Check out inPurgeable flag. It has also been recommended by the facebook engineering blog


  1. 如果你正在装载远程图像, ImageLoader的库是一个公平的一块code的退房,也可以用来处理OOM。您还可以查看毕加索

  1. If you're loading remote images, the ImageLoader Library is a fair piece of code to check out, which also handles oom. You can also check out Picasso.

如果您使用的是以前是一个推荐的选项,试图找到办法解决它,因为它实际上会为每个图像要分配更多的内存inPurgable标志。

If you're using the inPurgable flag that used to be a recommended option, try to find a way around it as it actually causes more memory to be allocated for each image.

如果你去code小,本地资产频繁,考虑保存自己的可绘制在一个HashMap并在需要时再取它们。少GC。

If you decode small, local assets frequently, consider saving your drawables in a hashmap and fetching them again when needed. Less GC.

如果你关心你的子类应用程序,您可以使用的 OnLowMemory 打电话,知道什么时候你可能的真正的需要清理(主要是适合调试,并不是真实的生活情况)......如果这不是太晚了? :)

If you care to subclass your Application, you can use the OnLowMemory call to know when you probably really need to clean up (mostly good for debugging, not real life situations) ... If that's not too late... :)

看看克里斯·巴内斯的博客。 是一个pretty有趣内存缓存解决方案

Take a look at Chris Banes' blog. This is a pretty interesting memory cache solution

实施调用内存微调时即可

Implement a memory trimmer you call when you can

另外一个令人吃惊的优化是使用更小的物体时,你可以......想想你最小的数据模型和图像尺寸,并尝试对那些符合标准的API。

One other unsurprising optimization is to use smaller objects when you can... Think of your minimal data models and image sizes and try to have a conforming API for those.