我想从Arraylist
of创建大字符串Strings
。
arraylist
包含大文本文件的部分内容。
文件的原始大小最大为 5MB。
我已经使用以下方法来做到这一点。
private void createFilefromChunks(ArrayList<String> stringval2) {
try {
System.gc();
Runtime.getRuntime().gc();
StringBuilder completeFile = new StringBuilder(150);
String val = null;
for (Iterator<String> iterator = stringval2.iterator(); iterator.hasNext(); ) {
val = (String) iterator.next();
completeFile.append(val);
}
System.out.println(completeFile.toString());
}
catch(OutOfMemoryError e) {
e.printStackTrace();
}
}
上述方法适用于最大 1MB 的小文件,
但如果我的文件大小增加到 4MB,它会给我一个OutofmemoryException
以下是我的 Logcat 详细信息:
12-26 14:43:17.862: E/dalvikvm-heap(718): Out of memory on a 2100160-byte allocation.
12-26 14:43:17.862: I/dalvikvm(718): "main" prio=5 tid=1 RUNNABLE
12-26 14:43:17.862: I/dalvikvm(718): | group="main" sCount=0 dsCount=0 obj=0x40a14568 self=0x2a00b9e0
12-26 14:43:17.862: I/dalvikvm(718): | sysTid=718 nice=0 sched=0/0 cgrp=apps handle=1073870640
12-26 14:43:17.871: I/dalvikvm(718): | schedstat=( 16034835833 4219745479 1751 ) utm=1509 stm=94 core=0
12-26 14:43:17.871: I/dalvikvm(718): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~94)
12-26 14:43:17.871: I/dalvikvm(718): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
12-26 14:43:17.871: I/dalvikvm(718): at java.lang.StringBuilder.append(StringBuilder.java:216)
12-26 14:43:17.871: I/dalvikvm(718): at com.example.splitbychapter.MainActivity.createFilefromChunks(MainActivity.java:160)
12-26 14:43:17.871: I/dalvikvm(718): at com.example.splitbychapter.MainActivity.readAsperChapter(MainActivity.java:120)
12-26 14:43:17.871: I/dalvikvm(718): at com.example.splitbychapter.MainActivity.onCreate(MainActivity.java:50)
12-26 14:43:17.881: I/dalvikvm(718): at android.app.Activity.performCreate(Activity.java:5008)
12-26 14:43:17.881: I/dalvikvm(718): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-26 14:43:17.881: I/dalvikvm(718): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-26 14:43:17.881: I/dalvikvm(718): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-26 14:43:17.881: I/dalvikvm(718): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-26 14:43:17.881: I/dalvikvm(718): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-26 14:43:17.890: I/dalvikvm(718): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 14:43:17.890: I/dalvikvm(718): at android.os.Looper.loop(Looper.java:137)
12-26 14:43:17.890: I/dalvikvm(718): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-26 14:43:17.890: I/dalvikvm(718): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 14:43:17.890: I/dalvikvm(718): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 14:43:17.890: I/dalvikvm(718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-26 14:43:17.890: I/dalvikvm(718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-26 14:43:17.901: I/dalvikvm(718): at dalvik.system.NativeStart.main(Native Method)
12-26 14:43:17.901: W/System.err(718): java.lang.OutOfMemoryError
12-26 14:43:17.920: W/System.err(718): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
12-26 14:43:17.920: W/System.err(718): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
12-26 14:43:17.930: W/System.err(718): at java.lang.StringBuilder.append(StringBuilder.java:216)
12-26 14:43:17.930: W/System.err(718): at com.example.splitbychapter.MainActivity.createFilefromChunks(MainActivity.java:160)
12-26 14:43:17.940: W/System.err(718): at com.example.splitbychapter.MainActivity.readAsperChapter(MainActivity.java:120)
12-26 14:43:17.940: W/System.err(718): at com.example.splitbychapter.MainActivity.onCreate(MainActivity.java:50)
12-26 14:43:17.940: W/System.err(718): at android.app.Activity.performCreate(Activity.java:5008)
12-26 14:43:17.940: W/System.err(718): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-26 14:43:17.940: W/System.err(718): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-26 14:43:17.952: W/System.err(718): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-26 14:43:17.952: W/System.err(718): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-26 14:43:17.952: W/System.err(718): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-26 14:43:17.952: W/System.err(718): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 14:43:17.961: W/System.err(718): at android.os.Looper.loop(Looper.java:137)
12-26 14:43:17.961: W/System.err(718): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-26 14:43:17.971: W/System.err(718): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 14:43:17.980: W/System.err(718): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 14:43:17.980: W/System.err(718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-26 14:43:17.980: W/System.err(718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-26 14:43:17.991: W/System.err(718): at dalvik.system.NativeStart.main(Native Method)
它也将我的堆大小增加到 13MB 并抛出outOfMemoryError
.
StringBuilder
它在这一行引发错误completeFile.append(val);
任何帮助表示赞赏。
提前致谢。