android应用程序中的java.lang.OutOfMemoryError

时间:2014-08-29 11:38:35

标签: java android eclipse memory android-asynctask

        try {       
            Gson gson = new Gson();
            try
            {
                ((ProfileActivity)getActivity()).interactionResult = gson.fromJson(HomeCardString.toString(), InteractionSummaryResult.class);

            }
            catch(Exception e)
            {
                Log.i("myyyy", e.getMessage());
                e.printStackTrace();
            }

        } catch (Throwable t) {
            Log.e("My App", "Could not parse malformed JSON: \"" + responseString + "\"" + t.getMessage());
        }

gson.fromgson .....这一行在Throwable t ....创建例外。我在HomeCardString.toString()中的数据非常大。记事本文件中大约1.9MB。我无法减少该数据的大小。该怎么办?

08-29 16:34:36.963: E/AndroidRuntime(29319): FATAL EXCEPTION: main
08-29 16:34:36.963: E/AndroidRuntime(29319): java.lang.OutOfMemoryError
08-29 16:34:36.963: E/AndroidRuntime(29319):    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at java.lang.StringBuilder.append(StringBuilder.java:216)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at com.stalker.androidapp.InteractionTabFragment$ProfileInteractionCall.onPostExecute(InteractionTabFragment.java:175)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at com.stalker.androidapp.InteractionTabFragment$ProfileInteractionCall.onPostExecute(InteractionTabFragment.java:1)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at android.os.AsyncTask.finish(AsyncTask.java:602)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at android.os.Looper.loop(Looper.java:137)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at android.app.ActivityThread.main(ActivityThread.java:4448)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at java.lang.reflect.Method.invokeNative(Native Method)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at java.lang.reflect.Method.invoke(Method.java:511)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
08-29 16:34:36.963: E/AndroidRuntime(29319):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

您可以尝试在清单中为您的应用设置android:largeHeap="true"。以下是参考资料:

http://developer.android.com/reference/android/R.styleable.html#AndroidManifestApplication_largeHeap

如果这不起作用,请看一下这篇文章:

JAVA - Best approach to parse huge (extra large) JSON file

答案 1 :(得分:0)

更改

        Log.e("My App", "Could not parse malformed JSON: \"" + responseString + "\"" + t.getMessage());

        String res = responseString.length() > 160 
            ? responseString.substring(0, 40) + "..." : responseString;
        Log.e("My App", "Could not parse malformed JSON: \"" + res + "\":\n" + t.getMessage());

因为日志记录并不重要。