Error Java heap space

3

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-/10.199.97.50:8043-26) JBWEB000236: Servlet.service() for servlet spring-servlet threw exception: java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:2786) [rt.jar:1.6.0_45]
        at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:133) [rt.jar:1.6.0_45]
        at org.apache.commons.lang3.SerializationUtils.serialize(SerializationUtils.java:180) [commons-lang3-3.3.2.jar:3.3.2]
        at org.apache.commons.lang3.SerializationUtils.clone(SerializationUtils.java:82) [commons-lang3-3.3.2.jar:3.3.2]
        at com.ibc.part.component.Filtros.filtra(Filtros.java:54) [classes:]
        at com.ibc.part.mvc.SaldosController.index(SaldosController.java:62) [classes:]
        at sun.reflect.GeneratedMethodAccessor304.invoke(Unknown Source) [:1.6.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_45]
        at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_45]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [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 = it.next();


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

            Bkft08 t08 = it2.next();

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

        }

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

    }

    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
source

3 answers

0

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
source
3

You can try to increase the memory for JVM for example something like -Xmx1024m , you have a file for memory allocation as server setenv.sh , 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:

$CATALINA_HOME/bin/setenv.sh  
 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:

Settings

Tomcat

    
answered by 28.02.2016 в 19:04
0

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