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 8202863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:20:33+00:00 2026-06-07T07:20:33+00:00

I have this piece of code that converts an ArrayList of StringBuilders into an

  • 0

I have this piece of code that converts an ArrayList of StringBuilders into an ArrayList of Strings:

Code:

public ArrayList<String> convGenSeqToString(ArrayList<StringBuilder> buff){
        ArrayList<String> convBuf = new ArrayList<String>();
        
        for (StringBuilder xVar: buff){
            convBuf.add(xVar.toString());
        }
        return convBuf;
    }

My code just works fine for files which are 15-20MB text files. However I have a text file which is 44MB and whenever I run my program with that text file I always get this error.

Error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2746)
    at java.util.ArrayList.ensureCapacity(ArrayList.java:187)
    at java.util.ArrayList.add(ArrayList.java:378)
    at Recognition.convGenSeqToString(Recognition.java:157)
    at Recognition.genSeq(Recognition.java:145)
    at Recognition.Recognitions(Recognition.java:96)
    at ChainDetection.main(Detection.java:25)

I already increased the memory in the JVM Run Configuration with -Xmx2048M and still I have the same error. I pinpointed the error to the code I displayed above and its highlighting this line:

convBuf.add(xVar.toString());

Are there any other ways to convert a StringBuilder to a string without using the .toString() method? I have seen in other forums and here as well that they created custom classes for toString but am not yet well versed with Generics and some "@" keywords. Anyone have suggestions or guidelines for how to solve this?

Edited

I edited my code as suggested by vanza
here it is:

public ArrayList<String> convGenSeqToString(ArrayList<StringBuilder> buff){
        ArrayList<String> convBuf = new ArrayList<String>(buff.size());
        Iterator <StringBuilder> iterBuf = buff.iterator();
        
        while (iterBuf.hasNext()){
            StringBuilder x = iterBuf.next();
            convBuf.add(x.toString());
            iterBuf.remove();
        }
        return convBuf;
    }

Error Message:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.ArrayList.<init>(ArrayList.java:132)
    at Recognition.convGenSeqToString(Recognition.java:154)
    at Recognition.genSeq(Recognition.java:145)
    at Recognition.Recognitions(Recognition.java:96)
    at ChainDetection.main(ChainDetection.java:25)

I tried this with buff.size() and without buff.size() and I got the same error message. But it seems like my error message decreased to 5 now. Any other thoughts on this? 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-07T07:20:35+00:00Added an answer on June 7, 2026 at 7:20 am

    Without knowing more about your program, I’ll refrain from making any comments about the approach you’ve taken and comment only on the code you’ve posted. It sounds weird to me that a 44MB file is filling a 2G heap, though.

    One thing you can do is allocate space in your target array up front:

    ArrayList<String> convBuf = new ArrayList<String>(buf.size());
    

    This will avoid the ArrayList resize step, which creates a copy of the existing list (and shows up in your stack trace).

    Another thing you can try is to release references to the original StringBuilders while building the String array. Use an Iterator (instead of a “for each” loop), and remove the StringBuilder from the buff array using Iterator.remove() on each iteration; that way you’ll free some memory for the garbage collector to reclaim if you’re running out of memory.

    But again, it sounds weird that you’re running out of memory with such a small file. Maybe looking at your heap with jvisualvm can shed some light.

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

Sidebar

Related Questions

I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have a piece of code that the ARC converter turned into this... //
I have this piece of code that works fine in subsonic 2.2, I migrated
I have this piece of code that is giving me trouble. I know all
I have this small piece of code that basically takes a list and runs
Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have a program I wrote in Windows with this piece of code that
I have this program that should execute a piece of code base on the
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a DSL - like piece of Ruby code that looks like this:

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.