Error Java heap space


In my web application, I miss this error, which causes me to restart the server:

13:08:13,508 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Particulares].[spring-servlet]] (http-/ JBWEB000236: Servlet.service() for servlet spring-servlet threw exception: java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf( [rt.jar:1.6.0_45]
        at [rt.jar:1.6.0_45]
        at org.apache.commons.lang3.SerializationUtils.serialize( [commons-lang3-3.3.2.jar:3.3.2]
        at org.apache.commons.lang3.SerializationUtils.clone( [commons-lang3-3.3.2.jar:3.3.2]
        at com.ibc.part.component.Filtros.filtra( [classes:]
        at com.ibc.part.mvc.SaldosController.index( [classes:]
        at sun.reflect.GeneratedMethodAccessor304.invoke(Unknown Source) [:1.6.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( [rt.jar:1.6.0_45]
        at java.lang.reflect.Method.invoke( [rt.jar:1.6.0_45]
        at [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle( [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod( [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal( [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle( [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]

and the code it refers to:

public PosicionGlobal2 filtra(PosicionGlobal2 pg, FiltroTarjeta filtro, boolean ctaEmpresa) throws CuentasEmptyException {

    PosicionGlobal2 clon = SerializationUtils.clone(pg);

    for(Iterator<Bkft07> it=clon.getBkft07s().iterator(); it.hasNext();) {

        Bkft07 t07 =;

        for(Iterator<Bkft08> it2=t07.getBkft08s().iterator(); it2.hasNext();) {

            Bkft08 t08 =;

            //Si no lo quiero....
            if (!this.getEstadoTarjeta(t08).equals(filtro) && !filtro.equals(FiltroTarjeta.TODAS)){


        //Si la cuenta esta vacia....
        //Si no queremos cuentas de empresa...
        if (t07.getBkft08s().isEmpty() || (!ctaEmpresa && t07.getIndnomemp().intValue()==1)){


    if (clon.getBkft07s().isEmpty()) throw new CuentasEmptyException();

    return clon;

According to the error, line 54 is where an object is cloned, which contains an array of a hibernate entity.

Any way to avoid the error or avoid cloning the object?

asked by davcas 28.02.2016 в 18:29

3 answers


In the end the problem was that an entity was cloned which, in turn, contained other entities and one of them had 2 fields blob with images, and this cloning was always done when doing login, so it filled the memory and the Garbage collector could not cope.

Those blob fields are unnecessary in the application, so I no longer charge them, and I do not like memory.

answered by 01.03.2016 / 00:39

You can try to increase the memory for JVM for example something like -Xmx1024m , you have a file for memory allocation as server , could you try something like this "CATALINA_OPTS=-Xmx1024m" .

Look for some file that is configured for your environment and how to extend the memory, such as this one:

 set JAVA_OPTS=-Xms512M -Xmx1024M

Or it may be this other

set "JAVA_OPTS=-Xms512M -Xmx1024M"

You can look at these links to get more information:



answered by 28.02.2016 в 19:04

To avoid that exception, if you are using JUnit and Spring try to add this in each test class:

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
answered by 15.01.2019 в 10:03