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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:54:26+00:00 2026-06-04T18:54:26+00:00

I’m not sure how to specifically deal with this(new to Java). Basically I have

  • 0

I’m not sure how to specifically deal with this(new to Java). Basically I have a program that generates a lot of data thats beyond my memory(for example, its 10 gigs data and I have 4 gigs of ram). I decided to fork a thread that takes the data and writes it to disk, although I know disk writes could never keep up with the process generating it, I was hoping my application can be bound to how quickly I can write to disk. But after a while I get heap outofmemory errors.

Here’s parts I think are relevant:
All data is to be written is put in this variable:

private static Queue<short[]> result =  new LinkedList <short[]> ();

Here’s the part that saves to file:

   static class SaveToFile extends Thread {


        public void run() {
                FileWriter bw = null;
                try {
                    bw = new FileWriter("output.csv");
                    Thread.sleep(500); //delay the start so the queue can have some data
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            System.out.println("size of results during execution is " + result.size());
            while(!result.isEmpty()) {
                short[] current = result.poll();
                try {
                    bw.write(Arrays.toString(current) + "," + "\n");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            try {
                bw.flush();
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("file writing is done");
        }
    }

I’m not sure what I’m doing wrong, do I need to block the result’s queue at a certain size so my process stops writing to it? or am I doing something wrong with the writing to file, I am showing the non-buffered version but I have tried bufferedWriter with the same result? I have observed that while the program is running the file size is 0, only once it crashes it seems to write..is it holding this in memory even without bufferedWriter and could that be causing the memory issue?

My idea was that as the SaveToFile thread clears the queue it makes more room for the other process to continue to write to it(these are the only threads I’m running, the main program and SaveToFile).

  • 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-04T18:54:29+00:00Added an answer on June 4, 2026 at 6:54 pm

    do I need to block the result’s queue at a certain size so my process stops writing to it?

    Yes, you do. The producer generating data faster than it can be written out is the most likely cause of your process running out of memory.

    Another issue is that LinkedList is not synchronized, so you need to use locking when using a LinkedList to pass data between threads.

    To limit the capacity, you can use ArrayBlockingQueue or LinkedBlockingQueue. As an added bonus, both are thread-safe and thus won’t require external synchronization.

    Finally, if your code is I/O-bound, as it appear to be, you will probably get relatively little benefit from splitting it into two threads. This is worth bearing in mind, since it could be that you’re introducing all this extra complexity for little or no benefit.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and

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.