GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||
GeezerVison is showing github.com — Aggressively remove PoolThreadCache references from its finalizer object by franz1981 · Pull Request #14155 · netty/netty Conversation
|
|
This is related quarkusio/quarkus#41607 and quarkusio/quarkus#41156 (comment) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@normanmaurer @chrisvest It would be great if we could just let |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
FYI @normanmaurer @chrisvest I've added 72c4731b0b767cfb1864c60a757944fd1bb2450a which push the approach further by enabling opt-in to NOT use finalizers for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
0850f3e to
72c4731
Compare
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I ran the non-broken NettyLeak1 reproducer from #12749, and it looks like the issue is resolved. So… controversial suggestion: maybe we don't need to make the leak check and can always clear the FreeOnFinalize.cache field.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Here's a fixed version of import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; public class NettyLeak0 { public static void main(String[] args) throws Exception { System.setProperty("io.netty.allocator.useCacheForAllThreads", "true"); System.setProperty("io.netty.allocator.maxOrder", "9"); Thread.setDefaultUncaughtExceptionHandler((t, e) -> { e.printStackTrace(); Runtime.getRuntime().halt(42); }); int bufSize = 32 * 1024; AtomicInteger longLiving = new AtomicInteger(); List<ByteBuf> longLivingSink = Collections.synchronizedList(new ArrayList<ByteBuf>()); ArrayBlockingQueue<Thread> threads = new ArrayBlockingQueue<>(64); for (int i = 0; true; i++) { Thread thread = new Thread(() -> { for (int x = 0; x < 256; x++) { if (x != 0) { // allocate different size classes to fill more thread caches for (int j = 0; j <= 10; j++) { ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer(bufSize >> j); buf.release(); } } else { // only one buf is alive after thread termination ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer(1); longLiving.incrementAndGet(); longLivingSink.add(buf); } } }); if (!threads.offer(thread)) { threads.take().join(); threads.put(thread); } thread.start(); if (i % 1000 == 0) { System.gc(); // to get a chance for io.netty.buffer.PoolThreadCache.finalize System.out.println(PooledByteBufAllocator.DEFAULT.metric()); System.out.println( "chunks count: " + PooledByteBufAllocator.DEFAULT.metric().directArenas().stream() .flatMap(m -> m.chunkLists().stream()) .mapToInt(m -> { int count = 0; for (PoolChunkMetric __ : m) { count += 1; } return count; }) .sum() ); int ll = longLiving.get(); System.out.println("long living: " + ll); // real size of a buf will be > 1b & < 1kb, but just estimate it as 1kb System.out.println("long living size (MB): " + ll / 1024); System.out.println(); longLivingSink.removeIf(buf -> buf.release() && decrement(longLiving)); } } } private static boolean decrement(AtomicInteger longLiving) { longLiving.addAndGet(-1); return true; } } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@normanmaurer wdyt about the @chrisvest suggestion?
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sounds good to me |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
PTAL @chrisvest @normanmaurer Not very happy that I've added a new sys prop here, but I want to keep it safe for existing users... |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
14cbf9d to
9375b45
Compare
That's good to me |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Motivation: If a cache's FastThreadLocalThread owner win the race to remove the cache, due to debugging capabilities, it's finalizer will still retain a strong reference to it, causing few classes to leak (and eventually, their ClassLoader). Despite we cannot avoid finalizers to wait the finalization pass, we can reduce the memory footprint of "leaked" instances before the finalization happen. Modification: non-debug early cache removal can remove the cache strong reference within FreeOnFinalize, making it an emtpy shell, eligible for GC. Result: Smaller memory footprint while waiting finalization to happen
9375b45 to
f618686
Compare
|
Let's see what the CI think @normanmaurer and thanks again! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Let me have a look for my use case before merging. Thanks! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I can confirm that I don't see the finalizers trying to load classes after my CL is closed, which looks promising! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…eads This is going to be useful once netty/netty#14155 lands in Quarkus.
|
So @normanmaurer , now just need CI and @chrisvest to be happy bout this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Windows seems the only one unhappy, likely for unrelated reasons; if you are good to go, I am done here @normanmaurer |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I am happy if @chrisvest is happy... @chrisvest PTAL again |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@normanmaurer I can merge it myself, ok @normanmaurer ? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@franz1981 sure... lets go. Please also cherry-pick it to the other related branches.. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…ect (#14155) Motivation: If a cache's FastThreadLocalThread owner win the race to remove the cache, due to debugging capabilities, its finalizer will still retain a strong reference to it, causing few classes to leak (and eventually, their ClassLoader). Despite we cannot avoid finalizers to wait the finalization pass, we can reduce the memory footprint of "leaked" instances before the finalization happen. Modification: non-debug early cache removal can remove the cache strong reference within FreeOnFinalize, making it an emtpy shell, eligible for GC. Result: Smaller memory footprint while waiting finalization to happen
There was an error while loading. Please reload this page.
hi @franz1981 , would you please tell me the reason why FastThreadLocalThread(cleanupFastThreadLocals == true) must use finalizer to clean their PoolThreadCache ? In my opinion, when the Why can't we just do this? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…eads This is going to be useful once netty/netty#14155 lands in Quarkus.
…eads This is going to be useful once netty/netty#14155 lands in Quarkus.
…eads This is going to be useful once netty/netty#14155 lands in Quarkus.
…eads This is going to be useful once netty/netty#14155 lands in Quarkus.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.