Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
Heap memory error in Java

I'm tinkering a bit with Java but have a lot of experience in some other languages. I have a test problem that I know solution to (and can easily produce in Python and C++). But running the following Java code gives an

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I'm wondering if I'm making a simple mistake, I would not expect the memory footprint of this program to be very large at all:

public static void main(String[] args) {
    ArrayList<Integer> longest_sequence = new ArrayList<>();
    ArrayList<Integer> this_sequence;
    int n = 0;

    for (int i = 1; i < 1000000; i++) {
        this_sequence = new ArrayList<Integer>();

        n = i;
        this_sequence.add(n);
        while (n != 1) {
            if (n % 2 == 0) {
                n = n / 2;
            }
            else {
                n = 3*n + 1;
            }
            this_sequence.add(n);
        }
        if (this_sequence.size() > longest_sequence.size()) {
            longest_sequence = this_sequence;
        }
    }

    System.out.println(longest_sequence.get(0));
    System.out.println(longest_sequence.size());

}

To clarify a bit more:

A new list is created in each iteration of the program. It is either kept by assigning the longest_sequence to it, or it is discarded and overwritten by a new list instance.

I'm guessing my assumptions about that are incorrect, and instances are being preserved? The size of the lists should not be a problem (about 500 elements for the largest one).

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

It will fail even if you increase your heap space.

For n = 113383, an operation make you go through Integer limit and n becomes negative and this is ending in a infite loop.

It works if you change Integer by Long.

over 4 years ago · Santiago Trujillo Report

0

I think the easiest to avoid this is to add memory using -Xmx flag

https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html here's official docs to read more about it

over 4 years ago · Santiago Trujillo Report

0

to change the VM for Eclipse you can change the amount of the MV from Windows> Preferences> Java> Installed JREs from there select the JRE and click edit, then write in the Default VM Arguments: to -Xmx1024M or any other amount of memory ...

Well, it's fairly self-explanatory: you've run out of memory.

You may want to try starting it with more memory, using the -Xmx flag, e.g.

java -Xmx2048m [whatever you'd have written before] This will use up to 2 gigs of memory.

See the non-standard options list for more details.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!