且构网

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

内存和活动::内存不足

更新时间:2022-06-27 00:09:27

内存是Android的一个非常棘手的问题。

Memory is a very tricky subject in Android.

每一个应用程序变得依赖于设备上的堆内存的限制。这堆内存是Dalvik的内存加上本机内存,你可以把它看成在 dumpsys meminfo中结果总列。在Dalvik内存处理一切,除了与位图,被分配在本机内存(这是真的为Android蜂窝版本之前)。

Every app gets a heap memory limit depending on the device. This heap memory is the dalvik memory plus the native memory, and you can see it as the total column in the dumpsys meminfo results. The dalvik memory deals with everything except with the bitmaps, that are allocated in the native memory (this is true for Android versions before Honeycomb).

说了这么多,我只能回答大家的一些问题:

Having said that I can only answer to some of your questions:

  1. 据我所知,Android将始终分配内存的位图,即使是相同的。因此,你的情况,每次活动你的背景分配内存。

  1. As far as I know, Android will always allocate memory for Bitmaps, even if they are the same. Therefore, in your case, every activity allocates memory for your background.

我不知道,如果它更好地与主题开展工作,你必须尝试。

I don't know if its better to work with themes, you'll have to try that.

在一方面,活动不回收,而该设备有足够的内存来处理下一个活动。每一个活动是从那里被回收,当你pressed后退按钮推到一堆。在Android的情况下,需要更多的内存,它会从一堆重新分配的内存中的一个活动(可以追溯到题号之一,也许这是不是共享内存的原因)。另外,您可以设置活动 launchMode 来改变这种行为(看看here).

On one hand, activities are not reclaimed while the device has enough memory to deal with the next activity. Every activity is pushed to a pile from where it is recovered when you pressed the back button. In case Android needs more memory it removes one activity from the pile deallocating its memory (going back to question number one, maybe this is the reason for not sharing memory). On the other hand, you can set the activities launchMode to change this behaviour (have a look here).

我觉得太不显示本机内存中的数据。使用 dumpsys meminfo中的本机列,看看有多少位图分配的内存,你有。

I think MAT doesn't show native memory data. Use dumpsys meminfo's native column to see how much allocated memory for Bitmaps you have.

我有困难的时候处理内存溢出的问题我自己。现在我有一个更清晰的概念  它是如何工作的,我能够处理大型文件的工作而不会耗尽内存。我会强烈建议这两种资源,帮助我很多:

I have had hard times dealing with OutOfMemory problems myself. Now I have a much more clear idea of how it works and I am able to work with large files without running out of memory. I would highly recommend these two resources that helped me a lot:

  • How to discover memory usage of my application in Android
  • Google I/O 2011: Memory management for Android Apps (extremely useful!)

祝你好运!