Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda