出于对Android的内存错误由于堆大小增加 [英] Out of Memory Error in android due to Heap Size Increasing

查看:158
本文介绍了出于对Android的内存错误由于堆大小增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了内存不足的错误。我工作的即时聊天应用。它工作正常,但是当我运行的应用程序1到2个小时设备上的堆大小增加,当它在16 MB的应用程序达到启动悬挂和后一段时间被崩溃,并显示的内存不足由于堆大小,因为产生的堆大小大于分配。

I am getting out of Memory error. I am working on live chat application. It is working fine but when I am running the application 1 to 2 hours on the device the heap size is increasing and when it reached at 16 MB application start hanging and being crashed after some time and showing out of memory due to heap size because the resultant heap size is greater than allocated.

我测试我的HTC Explorer应用程序。在我的应用程序的大部分活动都使用后台线程和我使用Asnyc任务。

I am testing my application on HTC Explorer. In my application most of the activities are using background thread and for that i am using Asnyc Task.

我收到错误如下所示。

04-30 16:53:14.658: E/AndroidRuntime(5707): FATAL EXCEPTION: MagentoBackground
04-30 16:53:14.658: E/AndroidRuntime(5707): java.lang.OutOfMemoryError: (Heap Size=20167KB, Allocated=16063KB, Bitmap Size=355KB)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.util.ByteArrayBuffer.<init>(ByteArrayBuffer.java:53)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.io.AbstractSessionInputBuffer.init(AbstractSessionInputBuffer.java:82)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:98)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:173)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-30 16:53:14.658: E/AndroidRuntime(5707):     at com.live2support.CustomHttpClient.executeHttpPost1(CustomHttpClient.java:163)

有堆大小的限制?我该如何解决我的问题?

Is there limit of heap size? how can i solve my issue?

推荐答案

您的问题有两个部分:

1)如何确定堆的大小,我的测试设备上?

1) How can I determine the size of the heap on my test device?

2)为何我的应用程序超出了我的堆的大小?

2) Why is my app exceeding the size of my heap?

对于问题1,你可以直接在code致电确定您的测试设备上堆的大小:

Regarding question 1, you can determine the size of the heap on your test device directly in your code by calling:

调用Runtime.getRuntime()maxMemory();

Runtime.getRuntime().maxMemory();

请参阅<一href="http://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android/9428660#9428660">this帖子关于该方法的其他信息,以及一些适用于各种设备例如堆大小。

See this post for additional information on that method, as well as some example heap sizes available on various devices.

另外,如果你正在运行一个根深蒂固的设备,有可能是一种方法来直接设置(检查)通过接口堆大小。例如,在的CyanogenMod的各种版本的Andr​​oid,从设置菜单中,您可以选择CyanogenMod设置,然后性能,然后在虚拟机堆大小,并直接查看(其他城市)堆大小为您的设备。要小心,因为设置的堆大小太小,可以让您的设备表现不好或者更糟。

Also, if you are running a rooted device, there may be a way to directly set (and check) the heap size via the interface. For example, in CyanogenMod's various versions of Android, from the Settings menu, you can select "CyanogenMod settings," and then "Performance," and then "VM heap size," and directly view (and change) the heap size for your device. Be careful, as setting the heap size too small can make your device misbehave or worse.

对于问题2:您没有提供足够的信息来诊断您的具体问题,在任何情况下,做这样的诊断二手充其量是困难的。你最好的解决这一问题(和学习的东西持久的过程值)的选择将是熟悉一些可在Android上非常强大的内存分析工具(其中一些也被集成到Eclipse IDE)。我使用的Eclipse这些工具,所以这就是我将在下面说明。

Regarding question 2: You have not provided enough information to diagnose your specific problem, and in any case, doing such a diagnosis second-hand is difficult at best. Your best bet for resolving this issue (and for learning something of lasting value in the process) would be to become familiar with some of the very powerful memory analysis tools available in Android (some of which are also integrated into the Eclipse IDE). I use these tools from Eclipse, so that is what I will describe below.

首先,请确保您的Eclipse版本是最新的由安装Eclipse(如靛蓝)的最新版本。

First of all, make sure your Eclipse version is up to date by installing the latest version of Eclipse (e.g., Indigo).

接下来,在Eclipse中,选择帮助/安装新软件,然后单击下拉顶部并选择

Next, in Eclipse, select Help/Install New Software, and then click the dropdown at the top and select

"Indigo - http://download.eclipse.org/releases/indigo"

接下来,按一下旁边的加号打开通用工具类,并选择内存分析器,也内存分析器(图表)[可选]。安装这些工具。

Next, open the General Purpose Tools category by clicking the plus sign next to it, and select Memory Analyzer and also Memory Analyzer (Charts) [optional]. Install these tools.

接下来,选择Window / preferences,然后安卓/ DDMS,并选择HPROF行动在Eclipse中打开。这将导致你从DDMS生成是为Eclipse相应的格式的任何HPROF堆转储文件,也将导致其在(这是正上方安装)Eclipse的内存分析器会自动打开。

Next, select Window/Preferences, and then Android/DDMS, and select the HPROF action as "Open in Eclipse." This will cause any HPROF heap dump file that you generate from DDMS to be of the appropriate format for Eclipse, and will also cause it to be automatically opened in Eclipse's Memory Analyzer (which was just installed above).

现在,打开DDMS选择Window /打开透视图/其它/ DDMS。选择设备图标(看起来像一个电话)左侧拖动产生的窗口,以便它停靠的地方,你可以很容易地看到它。

Now, open DDMS by selecting Window/Open Perspective/Other/DDMS. Select the Devices icon (looks like a phone) on the left and drag the resulting window so that it is docked somewhere you can see it easily.

请确保您的设备通过USB连接到PC,并且您的应用程序正在运行。

Make sure that your device is connected to the PC via USB, and that your app is running.

在您刚才创建的设备选项卡,选择您正在运行的应用程序的过程。运行该应用程序,以它已经占据了足够的内存,你知道它已经泄露了点,但没有这么多,它崩溃。现在,单击设备选项卡中转储HPROF文件图标。经过短暂的延迟,你会在你的堆提供的报告进行选择。尝试泄漏嫌疑人报告上手。该报告将在内存分析工具打开。它会告诉你,你的应用程序使用的内存。检查各类对象,看看他们显得臃肿相关数据,你会期望他们所需要的量;如果是这样,可指示泄漏。

In the Devices tab that you have just created, select your running app's process. Run the app to the point where it has occupied enough memory that you know it has leaked, but not so much that it crashes. Now, click the Dump HPROF File icon in the devices tab. After a brief delay, you'll be offered a choice of reports on your heap. Try the Leak Suspects Report to get started. This report will open in the Memory Analysis Tool. It tells you where your app is using memory. Check the various kinds of objects and see if they look bloated relative to the amount of data that you would expect them to require; if so, that may indicate a leak.

这里是一个很好的教程描述更详细的如何生成和使用DDMS和内存分析工具探索您的堆。

Here is a nice tutorial describing in more detail how to generate and explore your heap using DDMS and the Memory Analyzer Tool.

早在DDMS(或DDMS透视图在Eclipse),您可以从设备选项卡中选择分配跟踪标签,而你的设备连接,然后将您的设备,然后从列表中该设备选择应用程序的过程。然后,在分配追踪器选项卡上,单击开始跟踪按钮,然后运行你的应用程序的相关操作(那些你怀疑正在泄漏),然后单击获取分配按钮,然后选择停止跟踪按钮。

Back in DDMS (or the DDMS perspective in Eclipse), you can select the Allocation Tracker tab while your device is attached, and then your device from the devices tab, and then select your app's process from the list for that device. Then, in the Allocation Tracker tab, click the Start Tracking button, and then run your app's relevant operations (those that you suspect are leaking), and then click the Get Allocations button, and then select the Stop Tracking button.

这将显示有,而你正在跟踪发生的所有分配(有限制它将存储量)。点击其中的任何一个会带你到堆栈分配的时间,并点击任何部分堆栈转储将带您参与分配的源$ C ​​$ C。

This will display all allocations that have occurred while you were tracking (there are limits to the amount it will store). Clicking on any one of these will take you to the stack at the time of allocation, and clicking on any part of that stack dump will take you to the source code that was involved in the allocation.

这些工具应该给你一些洞察可能会导致您的应用程序出现内存泄漏。

These tools should give you some insight into what might be causing your app to leak memory.

这篇关于出于对Android的内存错误由于堆大小增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆