如何了解 Android 2.* 位图有多少可用堆大小?

发布于 2024-11-14 05:44:53 字数 894 浏览 5 评论 0原文

我们的应用程序使用了大量的位图。例如,它在 G1、XOOM 上运行良好。但在 HTC Desire 上出现 OutOfMemory 错误。在代码中,我们使用 try/catch(OutOfMemoryError e) 并且所有设备(Desire 除外)都会抛出异常,但 HTC 只是杀死应用程序而不会出现 OOM 异常。我们将位图内存限制为 12 Mb,似乎该解决方案解决了问题,但客户在 HTC Desire HD 上仍然遇到问题。即使有 12 Mb 限制,也会出现 OOM。以下是日志:

06-07 12:03:43.978 E/dalvikvm-heap( 29616):1140128-byte external allocation too large for this process. 
06-07 12:03:43.978 E/dalvikvm( 29616):Out of memory: Heap Size=12311KB, Allocated=9420KB, Bitmap Size=12139KB, Limit=21884KB
06-07 12:03:43.978 E/dalvikvm( 29616):Trim info: Footprint=15751KB, Allowed Footprint=15751KB, Trimmed=3440KB
06-07 12:03:43.978 E/GraphicsJNI( 29616):VM won't let us allocate 1140128 bytes

据我所知,设备有不同的堆大小限制(G1:16mb,Droid:24mb,Xoom 48mb)。在我看来,系统应该至少提供 16 mb,但我们有 12 mb 的 OOM。我的问题是:如何找出适用于 Android 2.* 的位图有多少可用堆大小?或者请建议如何通过其他方式避免此类问题。仅供参考,我们不能使用更少的位图,尤其是当它在其他设备上运行良好时。预先感谢您的任何帮助!

Our application uses a lot of bitmaps. It works fine for example on G1, XOOM. But on HTC Desire there is an OutOfMemory error. In code we use try/catch(OutOfMemoryError e) and all devices(except Desire) throw exception, but HTC just kills the application without OOM exception. We restricted the memory for bitmaps to 12 Mb and it seemed that this solution fixed the problem, but customer still has problem on HTC Desire HD. There is OOM even with 12 Mb restriction. Here are the logs:

06-07 12:03:43.978 E/dalvikvm-heap( 29616):1140128-byte external allocation too large for this process. 
06-07 12:03:43.978 E/dalvikvm( 29616):Out of memory: Heap Size=12311KB, Allocated=9420KB, Bitmap Size=12139KB, Limit=21884KB
06-07 12:03:43.978 E/dalvikvm( 29616):Trim info: Footprint=15751KB, Allowed Footprint=15751KB, Trimmed=3440KB
06-07 12:03:43.978 E/GraphicsJNI( 29616):VM won't let us allocate 1140128 bytes

AFAIK there are different heap size limitations for devices(G1: 16mb, Droid: 24 mb, Xoom 48 mb). In my opinion system should give at least 16 mb, but we have OOM with 12mb. My question is: How to find out how many free heap size available for bitmap for Android 2.*? Or please advice how to avoid such problem in other ways. FYI we can't use less bitmaps, especially when it works fine on other devices. Thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-11-21 05:44:53

您可以尝试

Runtime.getRuntime().maxMemory();

或 Activity 的方法

getMemoryInfo(ActivityManager.MemoryInfo;

此外,您可以重写 Activity 的

onLowMemory();

方法,在该方法中您可以处理当 Activity 收到内存问题通知时在关闭之前会发生的情况。您还应该检查这个答案< /a> 来自 Android/Linux 内存上的 Android 开发人员。

You can try

Runtime.getRuntime().maxMemory();

or Activity's method

getMemoryInfo(ActivityManager.MemoryInfo;

Besides, you can override Activity's

onLowMemory();

method, in which you can handle what will happen when the Activity get's notified of the memory issue, before being shut down. And you should also check this answer from an Android dev on Android/Linux memory.

心在旅行 2024-11-21 05:44:53

此 OutOfMemory 异常来自内存泄漏,而不是文件本身。您是否重写了 onStop、onResume、onDestroy 等 Activity 生命周期方法?

This OutOfMemory exception comes from a memory leak not from the file itself. Are you overriding the activity lifecycle methods like onStop, onResume, onDestroy?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文