刷新数据时Android OutOfMemoryError

标签 android httpclient out-of-memory

在我的应用程序中,我每 3 分钟刷新一次数据。如果应用程序可以工作几个小时,我会遇到这样的错误:

java.lang.OutOfMemoryError
at org.apache.http.util.CharArrayBuffer.<init>(CharArrayBuffer.java:55)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:131)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:146)
at com.tab.Api.friends(Api.java:99)
at com.tab.TabLayout.updateFriends(TabLayout.java:692)
at com.tab.TabLayout.access$25(TabLayout.java:690)
at com.tab.TabLayout$7.run(TabLayout.java:684)
at java.lang.Thread.run(Thread.java:1102)

虽然我的代码在这个:“at com.tab.Api.friends(Api.java:99)” 地方看起来像这样:

    String result = "";
    JSONObject jArray = null;
    JSONArray friends = null;

    DefaultHttpClient client = new DefaultHttpClient();   
    try
    {
        HttpGet getFriends = new HttpGet("http://##########"+fb);
        HttpResponse getResponseFriends = client.execute(getFriends);
        HttpEntity getFriendsEntity = getResponseFriends.getEntity();                      
        if (getFriendsEntity != null) 
            result= EntityUtils.toString(getFriendsEntity);   
        try
        {
            jArray = new JSONObject(result);
        }
        catch(JSONException e)
        {

        }
    }
    catch(Exception e)
    {   
        Log.d("LOADING ERROR","Friends section");
    }

最佳答案

你的某处有内存泄漏。在某个地方,您持有对长期对象中内存密集型数据的引用。这样 GC 就无法收集这些数据。您的应用程序运行的时间越长,就会使用更多的内存,直到所有内存都用完。然后你的应用程序被 OutOfMemory Exception 杀死

可能对您有帮助的链接:

关于刷新数据时Android OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8444849/

相关文章:

Android if else 语句在 onItemClickListener 中不起作用

android - 在 ActivityGroup 中 onActivityResult 不起作用?

android - Robolectric 2.x + Jenkins 上的 Maven 因 APKLIB 依赖项而失败

c# - 使用 HttpClient 的进度信息

c# - HttpClient 无法与 HTTPs 端点握手

java - 大字符串中最长的重复且不重叠的子字符串

android - 避免位图内存不足错误的建议

android - Recyclerview 向上滑动以在 API 21 上的 android 中刷新

c# - 在 HttpClient 中传递 HttpMessageHandler 的正确方法是什么?

.net - .NET 堆的约 50% 的空闲对象。如何知道是什么导致了 OOM