java.lang.outofmemoryerror android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27916019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.outofmemoryerror android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
提问by Ahmad Arslan
while setting the content view I have got this exception. I have already tried to handle this exception by:
在设置内容视图时,我遇到了这个异常。我已经尝试通过以下方式处理此异常:
try{
setContentView(R.layout.activity_main);
}catch (OutOfMemoryError e) {
e.printStackTrace();
}
This exception came 5 out of 100 time approx while testing. but I am unable to resolve this issue. Or otherwise my QA team will kill me on this :(
测试时大约有 100 次中有 5 次出现此异常。但我无法解决这个问题。否则我的 QA 团队会在这方面杀死我 :(
回答by Jim
There are several ways to resolve this type of error. Usually it is because your image resolution is too high on your layout. Here are your options:
有多种方法可以解决此类错误。通常是因为您的布局上的图像分辨率太高。以下是您的选择:
Ask for a lower resolution image (fewer pixels). That may not be acceptable, but your team should be conscious of memory constraints.
Remove the image(s) from the layout, but leave the
ImageView
elements, if there are any. IF it is a background, remove the background. Then use java to load the images efficiently. You will need to measure the screen size and/or size of the elements you want the images for before loading. Android docs have a great example here:
要求较低分辨率的图像(更少的像素)。这可能是不可接受的,但您的团队应该意识到内存限制。
从布局中删除图像,但保留
ImageView
元素(如果有)。如果是背景,请删除背景。然后使用java高效加载图像。在加载之前,您需要测量屏幕尺寸和/或您想要图像的元素的尺寸。Android 文档在这里有一个很好的例子:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
- Increase the "heapSize" for your app. This is a LAST RESORT! Otherwise, you will likely run into the same error in the future and then have to resolve it using one of the above methods... and your QA really won't like you then! Here is a reference on
heapSize
:
- 为您的应用增加“heapSize”。这是最后的度假胜地!否则,您将来可能会遇到同样的错误,然后必须使用上述方法之一来解决它……而您的 QA 真的不会喜欢您!这是一个参考
heapSize
:
How to increase heap size of an android application?
It is also possible that you are referencing images on another screen or otherwise have a memory leak, in which case you need to review any/all code that is called before this (including other activities) to find where those leaks are. Likely causes are things like passing an Activity
to a background thread or service instead of passing it's Context only, creating static references to images, not using WeakReference
to handle large memory objects passed to other threads/processes, or other unusual approaches to references to objects.
您也可能在另一个屏幕上引用图像或以其他方式存在内存泄漏,在这种情况下,您需要查看在此之前调用的任何/所有代码(包括其他活动)以找到这些泄漏的位置。可能的原因是诸如将 an 传递Activity
给后台线程或服务而不是仅传递它的上下文、创建对图像的静态引用、不WeakReference
用于处理传递给其他线程/进程的大内存对象,或其他不寻常的对象引用方法。
回答by pvn
As mentioned by others this is happening because the app is having memory leak or your app is using too much memory.
正如其他人所提到的,这是因为应用程序存在内存泄漏或您的应用程序使用了太多内存。
Try to reduce the number of layers in your layout file first, this would most probably resolve the issue. In case this doesn't install MAT plugin, using that you can check the amount of memory being used by your app at runtime. There may be a possibility that a previous activity opened before your activity is not releasing memory or views are not being properly recycled in list/grid
首先尝试减少布局文件中的层数,这很可能会解决问题。如果这没有安装 MAT 插件,您可以使用它检查应用程序在运行时使用的内存量。可能有可能在您的活动未释放内存或视图未在列表/网格中正确回收之前打开的先前活动
PS : Reduce the number of layers in your layout file that would work most probably
PS:减少布局文件中最有可能工作的层数
回答by mavarazy
Provided code is not necessarily the cause of OutOfMemoryError. The primary cause for OutOfMemory is that JVM GC can't free enough memory for your operation to proceed.
提供的代码不一定是 OutOfMemoryError 的原因。OutOfMemory 的主要原因是 JVM GC 无法释放足够的内存供您继续操作。
Assuming setContentView is indeed heavy operation, and decreasing resolution size of the image, would solve it for a while, this problem would reappear in a different place in the system, in a different time.
假设setContentView确实是繁重的操作,降低图片的分辨率大小,会解决一段时间,这个问题会在系统的不同地方,不同时间再次出现。
You can google the reasons why GC can't allocate enough space, usually it means you have some stale reference in your code, which you need to clean, in order to help your GC.
您可以通过谷歌搜索 GC 无法分配足够空间的原因,通常这意味着您的代码中有一些陈旧的引用,您需要清理这些引用,以帮助您的 GC。
I would suggest using a profiler for your application, to track how space is allocated and cleared in your application. You have standard one by default with android.
我建议为您的应用程序使用分析器,以跟踪应用程序中空间的分配和清除方式。默认情况下,您有 android 的标准版本。