GeezerVison is showing github.com — 4.1.114.Final upgrade 4.1.115.Final OOM · Issue #14553 · netty/netty
4.1.114.Final upgrade 4.1.115.Final OOM #14553DescriptionExpected behavior
No OOM
Actual behavior
OOM
Steps to reproduce
Hard to provide
Minimal yet complete reproducer code (or URL to code)
Hard to provide
Netty version
4.1.115
JVM version (e.g. java -version)
Jdk17
OS version (e.g. uname -a)
K8s
I think this is a hard to reproduce issue, when i upgrade netty to 4.1.115.Final, the application of our cluster 2 node oom killed;
one is aws ec2, one is k8s node, i found the oom reason is mmap cache is increaseing slow, but not all the instance occurs this issue;
es2 monitor:

k8s node oom log:
i make sure our app'mmap do not increase;

I'd like to confirm if there was any change in version 4.115 that caused the mmap to grow? Activitychrisvest commented on Dec 10, 2024 List of changes: netty-4.1.114.Final...netty-4.1.115.Final
We don't directly map files, but anonymous mapped memory is where the system malloc sources its memory.
Maybe some of the adaptive allocator changes behaves poorly? linking12 commented on Dec 11, 2024
List of changes: netty-4.1.114.Final...netty-4.1.115.Final
We don't directly map files, but anonymous mapped memory is where the system malloc sources its memory. Maybe some of the adaptive allocator changes behaves poorly?
@chrisvest i found inactive_anno & rss memory cause the k8s node oom, i think adaptive has some memory leak in 4.1.115.Final version, also this issue happened on high traffic comming, but when no high traffic, everything is fine linking12 commented on Dec 12, 2024 Another node has OOM for the same reason. I turned on Leak Detaction and found no leak. I suspect there is a problem with adaptive allocation. I will roll to version 114 for verification. He-Pin commented on Dec 12, 2024 @linking12 Hi, which BytebufAllocator are you currently using? We found the same issue when we tried the AdaptiveByteBufAllocator when stressing testing, but sorry, have not had another chance to stress it again.
I think you can try to stress it with a different payload to see if that still happen in production. linking12 commented on Dec 12, 2024
@linking12 Hi, which BytebufAllocator are you currently using? We found the same issue when we tried the AdaptiveByteBufAllocator when stressing testing, but sorry, have not had another chance to stress it again.
I think you can try to stress it with a different payload to see if that still happen in production.
AdaptiveByteBufAllocator, 4.1.114.Final i think there is no this issue, i want to found what have changed in 4.1.115.Final franz1981 commented on Dec 12, 2024 Mmap Is used by glibc malloc when the allocations exceed MMAP_THRESHOLD; which native allocator you are using?
I suggest to use async profiler with -e mprotect if is glibc and we can probably capture the code path which keep on leaking linking12 commented on Dec 12, 2024
Mmap Is used by glibc malloc when the allocations exceed MMAP_THRESHOLD; which native allocator you are using? I suggest to use async profiler with -e mprotect if is glibc and we probably capture the code path which keep on leaking
We use the default glibc malloc of the operating system. I am currently rolling back the version to stop the bleeding. I will try your suggestion. franz1981 commented on Dec 12, 2024 Another option could be to use cleaners or limit the JVM native memory (you can do it both via a netty property or maxDirectMemory one on the JVM) and add heap dump on OOM so we can inspect the Chunk(s) instances there; my gut feeling point to #14550 (comment) which shows that over a specific requested size we allocates instead of reusing memory..
Re -e mprotect; glibc malloc creates new malloc arenas and perform mprotect on them, which means that the stack traces we can find can both be cause or an effect of a leak, but since allocations on the pool should become zero (if not on chunk exhaustion or big allocations), it's fine to be suspicious of any new allocation when the existing malloc arenas are exhausted. Reacted by linking12 and He-Pin(kerr) He-Pin commented on Dec 12, 2024 @linking12 Add a -XX:MaxDirectMemorySize=6g in your staging ENV and do stressing, which will help.
And another thing is , you can change your JVM to use jemalloc too. laosijikaichele commented on Dec 12, 2024 This may be related or not, I also have observed the OutOfMemoryError when doing the benchmark for AdaptiveByteBufAllocator, it seems that the AdaptiveByteBufAllocator is more likely to encounter 'OOM' than the PooledByteBufAllocator.DEFAULT allocator.
Use this sample code BufAllocatorOOMBenchmark to produce OutOfMemoryError, which limit the memory size by -Xms168m -Xmx168m -XX:MaxDirectMemorySize=168m
Run the sample code, you will see the PooledByteBufAllocator.DEFAULT passed the benchmark, but the AdaptiveByteBufAllocator got OutOfMemoryError.
For this sample code, the netty-version does not matter, '4.1.114.Final' and '4.1.115.Final' both rise the OutOfMemoryError. linking12 commented on Dec 13, 2024
Mmap Is used by glibc malloc when the allocations exceed MMAP_THRESHOLD; which native allocator you are using? I suggest to use async profiler with -e mprotect if is glibc and we probably capture the code path which keep on leaking
We use the default glibc malloc of the operating system. I am currently rolling back the version to stop the bleeding. I will try your suggestion.
rollback to '4.1.114.Final' oom disappear, but some node's memory has increased a little, maybe @laosijikaichele is right,it seems that the AdaptiveByteBufAllocator is more likely to encounter 'OOM' than the PooledByteBufAllocator.DEFAULT allocator. We usually release once every two weeks has covered up this issue He-Pin commented on Dec 13, 2024 He-Pin commented on Dec 13, 2024 @laosijikaichele I think the AdaptiveByteBufAllocator may have many small fragmants which are not fully utilized when allocation, I was not have time to look into it :( laosijikaichele commented on Dec 13, 2024 It seems that add the code
chunk.allocatedBytes = chunk.allocatedBytes - length;
after the line-1273 make the OOM less occurred:
|
@Override |
|
protected void deallocate() { |
|
if (chunk != null) { |
|
chunk.release(); |
|
} |
|
tmpNioBuf = null; |
|
chunk = null; |
|
rootParent = null; |
|
if (handle instanceof EnhancedHandle) { |
|
EnhancedHandle<AdaptiveByteBuf> enhancedHandle = (EnhancedHandle<AdaptiveByteBuf>) handle; |
|
enhancedHandle.unguardedRecycle(this); |
|
} else { |
|
handle.recycle(this); |
|
} |
|
} |
|
} |
But, I am not sure if this is correct in logical. @chrisvest @franz1981 @He-Pinnormanmaurer commented on Dec 13, 2024
It seems that add the code chunk.allocatedBytes = chunk.allocatedBytes - length; after the line-1273 make the OOM less occurred:
|
@Override |
|
protected void deallocate() { |
|
if (chunk != null) { |
|
chunk.release(); |
|
} |
|
tmpNioBuf = null; |
|
chunk = null; |
|
rootParent = null; |
|
if (handle instanceof EnhancedHandle) { |
|
EnhancedHandle<AdaptiveByteBuf> enhancedHandle = (EnhancedHandle<AdaptiveByteBuf>) handle; |
|
enhancedHandle.unguardedRecycle(this); |
|
} else { |
|
handle.recycle(this); |
|
} |
|
} |
|
} |
But, I am not sure if this is correct in logical. @chrisvest @franz1981 @He-Pin
So before null out chunk ? johnou commented on Dec 13, 2024 @laosijikaichele looks like there is also an edge case in io.netty.buffer.AdaptivePoolingAllocator.Chunk#readInitInto where allocatedBytes is incremented but in the finally catch when deallocating b/c of failure it is not decremented.
Index: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java b/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
--- a/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java (revision 67c50a1647b5a1c7673a327c833799493f062b3e)
+++ b/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java (date 1734091751136)
@@ -880,6 +880,7 @@
// If chunk is not null we know that buf.init(...) failed and so we need to manually release
// the chunk again as we retained it before calling buf.init(...).
chunk.release();
+ allocatedBytes = startIndex;
}
}
}
Reacted by old driver, He-Pin(kerr) and linking12 normanmaurer commented on Dec 13, 2024
@laosijikaichele looks like there is also an edge case in io.netty.buffer.AdaptivePoolingAllocator.Chunk#readInitInto where allocatedBytes is incremented but in the finally catch when deallocating b/c of failure it is not decremented.
Index: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java b/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
--- a/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java (revision 67c50a1647b5a1c7673a327c833799493f062b3e)
+++ b/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java (date 1734091751136)
@@ -880,6 +880,7 @@
// If chunk is not null we know that buf.init(...) failed and so we need to manually release
// the chunk again as we retained it before calling buf.init(...).
chunk.release();
+ allocatedBytes = startIndex;
}
}
}
Good catch #14577 normanmaurer commented on Dec 13, 2024
@johnou Thanks for the quick test. But I thinks it's probably not that simple, as the #14553 (comment) stated, we may need to find another way to make the released memory be reusable...
Yes its not as easy... normanmaurer commented on Dec 13, 2024 So I think the problem basically steams from how we calculate the queue size:
/**
* The maximum size of a pooled chunk, in bytes. Allocations bigger than this will never be pooled.
* <p>
* This number is 10 MiB, and is derived from the limitations of internal histograms.
*/
private static final int MAX_CHUNK_SIZE =
BUFS_PER_CHUNK * (1 << AllocationStatistics.HISTO_MAX_BUCKET_SHIFT); // 10 MiB.
/**
* The capacity if the central queue that allow chunks to be shared across magazines.
* The default size is {@link NettyRuntime#availableProcessors()},
* and the maximum number of magazines is twice this.
* <p>
* This means the maximum amount of memory that we can have allocated-but-not-in-use is
* 5 * {@link NettyRuntime#availableProcessors()} * {@link #MAX_CHUNK_SIZE} bytes.
*/
private static final int CENTRAL_QUEUE_CAPACITY = SystemPropertyUtil.getInt(
"io.netty.allocator.centralQueueCapacity", NettyRuntime.availableProcessors());
This basically means if you end up with a Chunk size of 10MB the worst case is that you will have 2 * 10 MB (for the Magazine) + cores * 10MB. This can easily a few hundred MB. He-Pin commented on Dec 13, 2024 You mean if there are many cores ? normanmaurer commented on Dec 13, 2024 Yeah... that said there was also a bug that could lead to huge memory usage:
#14581 linking12 commented on Dec 18, 2024
@normanmaurer i'm not sure this issue is fixed, we use the latest code;
I found that the rss memory will continue to increase. Although it will decrease after a certain period of time, it is generally increasing. Now I understand why the mmap memory why increase. It should be that when the rss is not enough, it will try to exchange memory through mmap
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|