Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9072879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:16:43+00:00 2026-06-16T18:16:43+00:00

I want to create large string from Arraylist of Strings . The arraylist contains

  • 0

I want to create large string from Arraylist of Strings.

The arraylist contains parts of a big text file.

original size of file is upto 5MB.

I have used following way to do it.

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();
    }
}

Above method works fine for small files upto 1MB,

but if my file size is increased to 4MB, it will give me an OutofmemoryException

Following is my Logcat details:

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)

It grows my heap size upto 13MB as well and throws outOfMemoryError.

It throws error for StringBuilder at this line completeFile.append(val);

Any help is appreciated.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:16:44+00:00Added an answer on June 16, 2026 at 6:16 pm

    You’ll be able to create larger strings if you know how big you need them to be to start with – you can create the StringBuilder with the exact capacity required, so it will never need to expand. It’s during expansion that the memory requirements are largest. So:

    int size = 0;
    for (String value : stringval2) {
        size += value.length();
    }
    StringBuilder builder = new StringBuilder(size);
    for (String value : stringval2) {
        builder.append(value);
    }
    String x = builder.toString();
    

    Now the final conversion to String may still cause a problem – but if it does, there’s not a lot you’ll be able to do, to be honest.

    Also note that text in Java takes two bytes per character, so if this is being read from an ASCII file which is 5MB in size, you’ll end up with 10MB required for the string.

    Do you have to put all of this into a single string? Given that you’re running on mobile devices (by the looks of it) you should really be trying to think of ways to use less memory if you can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create an App that uses a potentially large xml file. It
I want to create a large database of GPS coordinates that can be queried
I want to create a large (~300,000 entries) List of self defined objects of
Basically I want to create one large object of many object in JavaScript. Something
I'm looking to create a fairly large-scale ASP.NET MVC project and I want to
i want create multiple search where statement $where_search is a multiple condition from post
i want create a custom json data from the mssql 2008 results so that
i want to return a string value from an anonymous method/delegate. How can i
I want to create a Excel.Range object from a list of cell references which
I have a large table of birthdays that I want to convert from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.