java.lang.OutOfMemoryError: Java heap space

Spring JIRA | Francisco Lozano | 2 years ago
  1. 0

    There are two inter-related issues here: 1. The maxRequestSize parameter name is incorrect, as it also affects the maximum response size (it's passed to HttpObjectAggregator): {code} private Bootstrap getBootstrap() { if (this.bootstrap == null) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (sslContext != null) { pipeline.addLast(sslContext.newHandler(channel.alloc())); } pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(maxRequestSize)); } }); {code} -Way to solve this would be to add a separate maxResponseSize parameter and use it in HttpObjectAggregator.- 2. Because of the issue above, when you increase the `maxRequestSize` parameter (to be able to process `maxRequestSize`-big responses), even if the request to be processed is just a few kB's, it will always allocate `maxRequestSize` for it. This can easily cause OOM errors - but it's not a leak, it's just an abusive use of memory. {code} java.lang.OutOfMemoryError: Java heap space at io.netty.buffer.UnpooledHeapByteBuf.<init>(UnpooledHeapByteBuf.java:45) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.UnpooledByteBufAllocator.newHeapBuffer(UnpooledByteBufAllocator.java:43) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:136) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:127) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.Unpooled.buffer(Unpooled.java:118) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at org.springframework.http.client.Netty4ClientHttpRequest.<init>(Netty4ClientHttpRequest.java:68) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEAS E] at org.springframework.http.client.Netty4ClientHttpRequestFactory.createRequestInternal(Netty4ClientHttpRequestFactory.java:148) ~[spring-web- 4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.http.client.Netty4ClientHttpRequestFactory.createAsyncRequest(Netty4ClientHttpRequestFactory.java:144) ~[spring-web-4.1 .4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.http.client.support.AsyncHttpAccessor.createAsyncRequest(AsyncHttpAccessor.java:76) ~[spring-web-4.1.4.RELEASE.jar:4.1. 4.RELEASE] at org.springframework.web.client.AsyncRestTemplate.doExecute(AsyncRestTemplate.java:533) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.client.AsyncRestTemplate.execute(AsyncRestTemplate.java:512) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.client.AsyncRestTemplate.exchange(AsyncRestTemplate.java:456) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] {code} The issue is here: {code} public Netty4ClientHttpRequest(Bootstrap bootstrap, URI uri, HttpMethod method, int maxRequestSize) { this.bootstrap = bootstrap; this.uri = uri; this.method = method; this.body = new ByteBufOutputStream(Unpooled.buffer(maxRequestSize)); } {code} The method used allocates always the max capacity: {code} /** * Creates a new big-endian Java heap buffer with the specified {@code capacity}, which * expands its capacity boundlessly on demand. The new buffer's {@code readerIndex} and * {@code writerIndex} are {@code 0}. */ public static ByteBuf buffer(int initialCapacity) { return ALLOC.heapBuffer(initialCapacity); } {code} Instead, this allocation method should be used: {code} /** * Creates a new big-endian Java heap buffer with the specified * {@code initialCapacity}, that may grow up to {@code maxCapacity} * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0}. */ public static ByteBuf buffer(int initialCapacity, int maxCapacity) { return ALLOC.heapBuffer(initialCapacity, maxCapacity); } {code} I set this bug as critical because, with current status, the client is unusable for any meaningful load. Aside from this, it'd be great if it was possible to provide the memory allocator, instead of using always the static Unpooled one...

    Spring JIRA | 2 years ago | Francisco Lozano
    java.lang.OutOfMemoryError: Java heap space
  2. 0

    There are two inter-related issues here: 1. The maxRequestSize parameter name is incorrect, as it also affects the maximum response size (it's passed to HttpObjectAggregator): {code} private Bootstrap getBootstrap() { if (this.bootstrap == null) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (sslContext != null) { pipeline.addLast(sslContext.newHandler(channel.alloc())); } pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(maxRequestSize)); } }); {code} -Way to solve this would be to add a separate maxResponseSize parameter and use it in HttpObjectAggregator.- 2. Because of the issue above, when you increase the `maxRequestSize` parameter (to be able to process `maxRequestSize`-big responses), even if the request to be processed is just a few kB's, it will always allocate `maxRequestSize` for it. This can easily cause OOM errors - but it's not a leak, it's just an abusive use of memory. {code} java.lang.OutOfMemoryError: Java heap space at io.netty.buffer.UnpooledHeapByteBuf.<init>(UnpooledHeapByteBuf.java:45) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.UnpooledByteBufAllocator.newHeapBuffer(UnpooledByteBufAllocator.java:43) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:136) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:127) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at io.netty.buffer.Unpooled.buffer(Unpooled.java:118) ~[netty-all-4.0.24.Final.jar:4.0.24.Final] at org.springframework.http.client.Netty4ClientHttpRequest.<init>(Netty4ClientHttpRequest.java:68) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEAS E] at org.springframework.http.client.Netty4ClientHttpRequestFactory.createRequestInternal(Netty4ClientHttpRequestFactory.java:148) ~[spring-web- 4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.http.client.Netty4ClientHttpRequestFactory.createAsyncRequest(Netty4ClientHttpRequestFactory.java:144) ~[spring-web-4.1 .4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.http.client.support.AsyncHttpAccessor.createAsyncRequest(AsyncHttpAccessor.java:76) ~[spring-web-4.1.4.RELEASE.jar:4.1. 4.RELEASE] at org.springframework.web.client.AsyncRestTemplate.doExecute(AsyncRestTemplate.java:533) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.client.AsyncRestTemplate.execute(AsyncRestTemplate.java:512) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.client.AsyncRestTemplate.exchange(AsyncRestTemplate.java:456) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] {code} The issue is here: {code} public Netty4ClientHttpRequest(Bootstrap bootstrap, URI uri, HttpMethod method, int maxRequestSize) { this.bootstrap = bootstrap; this.uri = uri; this.method = method; this.body = new ByteBufOutputStream(Unpooled.buffer(maxRequestSize)); } {code} The method used allocates always the max capacity: {code} /** * Creates a new big-endian Java heap buffer with the specified {@code capacity}, which * expands its capacity boundlessly on demand. The new buffer's {@code readerIndex} and * {@code writerIndex} are {@code 0}. */ public static ByteBuf buffer(int initialCapacity) { return ALLOC.heapBuffer(initialCapacity); } {code} Instead, this allocation method should be used: {code} /** * Creates a new big-endian Java heap buffer with the specified * {@code initialCapacity}, that may grow up to {@code maxCapacity} * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0}. */ public static ByteBuf buffer(int initialCapacity, int maxCapacity) { return ALLOC.heapBuffer(initialCapacity, maxCapacity); } {code} I set this bug as critical because, with current status, the client is unusable for any meaningful load. Aside from this, it'd be great if it was possible to provide the memory allocator, instead of using always the static Unpooled one...

    Spring JIRA | 2 years ago | Francisco Lozano
    java.lang.OutOfMemoryError: Java heap space
  3. 0

    OutOfMemory Exception : java heap space

    Google Groups | 6 months ago | deepak singh
    java.lang.OutOfMemoryError: Java heap space* *java.lang.OutOfMemoryError: Java heap space*
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    OutOfMemory Exception : java heap space

    Google Groups | 6 months ago | deepak singh
    java.lang.OutOfMemoryError: Java heap space* *java.lang.OutOfMemoryError: Java heap space*
  6. 0

    [exoplatform] [explatform] Memory requirements

    ow2.org | 1 year ago
    java.lang.OutOfMemoryError: Java heap space>java.lang.OutOfMemoryError: Java heap space

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.OutOfMemoryError

      Java heap space

      at io.netty.buffer.UnpooledHeapByteBuf.<init>()
    2. Netty
      Unpooled.buffer
      1. io.netty.buffer.UnpooledHeapByteBuf.<init>(UnpooledHeapByteBuf.java:45)[netty-all-4.0.24.Final.jar:4.0.24.Final]
      2. io.netty.buffer.UnpooledByteBufAllocator.newHeapBuffer(UnpooledByteBufAllocator.java:43)[netty-all-4.0.24.Final.jar:4.0.24.Final]
      3. io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:136)[netty-all-4.0.24.Final.jar:4.0.24.Final]
      4. io.netty.buffer.AbstractByteBufAllocator.heapBuffer(AbstractByteBufAllocator.java:127)[netty-all-4.0.24.Final.jar:4.0.24.Final]
      5. io.netty.buffer.Unpooled.buffer(Unpooled.java:118)[netty-all-4.0.24.Final.jar:4.0.24.Final]
      5 frames
    3. Spring
      AsyncRestTemplate.exchange
      1. org.springframework.http.client.Netty4ClientHttpRequest.<init>(Netty4ClientHttpRequest.java:68)[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
      2. org.springframework.http.client.Netty4ClientHttpRequestFactory.createRequestInternal(Netty4ClientHttpRequestFactory.java:148)[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
      3. org.springframework.http.client.Netty4ClientHttpRequestFactory.createAsyncRequest(Netty4ClientHttpRequestFactory.java:144)[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
      4. org.springframework.http.client.support.AsyncHttpAccessor.createAsyncRequest(AsyncHttpAccessor.java:76)[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
      5. org.springframework.web.client.AsyncRestTemplate.doExecute(AsyncRestTemplate.java:533)[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
      6. org.springframework.web.client.AsyncRestTemplate.execute(AsyncRestTemplate.java:512)[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
      7. org.springframework.web.client.AsyncRestTemplate.exchange(AsyncRestTemplate.java:456)[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
      7 frames