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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:10:04+00:00 2026-05-31T00:10:04+00:00

First of all I would try to explain what I need to do. I

  • 0

First of all I would try to explain what I need to do.
I need to read a file (whose size could be from 1 byte to 2 GB), 2 GB maximum because I try to use MappedByteBuffer for fast reading. Maybe later I will try to read file in chunks in order to read files of arbitrary size.

When i read file I convert its bytes and convert them (using ASCII encoding) to chars which later I put into a StringBuilder and then I put this String Builder into an ArrayList

However I also need to do the following:

  1. User could type blockSize which is the number of chars I have to read into the StringBuilder (which is basically number of file bytes converted to chars)

  2. Once I have collected the user defined char count, I create a copy of the String Builder and put it into an Array List

All steps are performed for every char read. The problem is with String Builder since if the file is big (<500 MB), I get the exception OutOfMemoryError.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:45)
    at java.lang.StringBuilder.<init>(StringBuilder.java:80)
    at java.lang.StringBuilder.<init>(StringBuilder.java:106)
    at borrows.wheeler.ReadFile.readFile(ReadFile.java:43)
Java Result: 1 

I post my code, maybe someone could suggest improvements to this code or suggest some alternatives.

public class ReadFile {

    //matrix block size
    public int blockSize = 100;

    public int charCounter = 0;

    public ArrayList readFile(File file) throws FileNotFoundException, IOException {

        FileChannel fc = new FileInputStream(file).getChannel();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());

        ArrayList characters = new ArrayList(); 
        int counter = 0;
        StringBuilder sb = new StringBuilder();//blockSize-1

        while (mbb.hasRemaining()) {   

        char charAscii = (char)mbb.get();


            counter++;
            charCounter++;

             if (counter == blockSize){

                sb.append(charAscii);
                characters.add(new StringBuilder(sb));//new StringBuilder(sb)
                sb.delete(0, sb.length());
                counter = 0;

            }else{

                sb.append(charAscii);

             }

         if(!mbb.hasRemaining()){
            characters.add(sb);
        }



        }
        fc.close();
        return characters;


    }

}

EDIT:
I am doing Burrows-Wheeler transformation. There i should read every file then by Block Size create as many as needed matrixes. well i believe that wiki will explain better than me:

http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform

  • 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-05-31T00:10:05+00:00Added an answer on May 31, 2026 at 12:10 am

    If you load large files, it’s not entirely surprising that you run out of memory.

    How much memory do you have? Are you on a 64-bit system with 64-bit Java? How much heap memory have you allocated (e.g using -Xmx setting)?

    Bear in mind that you will need at least twice as much memory as the filesize, because Java uses Unicode UTF-16, which uses at least 2 bytes for each character, but your input is one byte per character. So to load a 2GB file you will need at least 4GB allocated to the heap just for storing this text data.

    Also, you need to sort out the logic in your code – you do the same sb.append(charAscii) in the if and the else, and you test !mbb.hasRemaining() in every iteration of a while((mbb.hasRemaining()) loop.

    As I asked in your previous question, do you need to store StringBuilders, or would the resulting Strings be OK? Storing strings would save space because StringBuilder allocates memory in big chunks (I think it doubles in size every time it runs out of space!) so may waste a lot.

    If you do have to use StringBuilders then pre-sizing them to the value of blockSize would make the code more memory-efficient (and faster).

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

Sidebar

Related Questions

First of all I'm kind of new in Objc. (so try to explain me
at first I would like to try to explain my plan: Sometimes I just
first of all i would like to say i know its probably an easy
First of all, I'm really new to the MVC Asp.Net ideology. I would like
First of all, is this possible? If so: What challenges would I encounter in
I would like all my URLs in codeigniter to start with the first URI
First, I would to thank everyone for all the help they provide via this
I would like to produce a character list of all of the first letters
First of all, I'm quite new to the Android and JAVA world (coming from
First of all, I should explain what I'm trying to do first. I'm creating

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.