wy96f commented on issue #2832: ARTEMIS-2482 Large messages could leak native 
ByteBuffers
URL: https://github.com/apache/activemq-artemis/pull/2832#issuecomment-531654209
 
 
   @franz1981 
   
   > I have to add more detail and I was wrong: the tiny/small/medium thread 
local caches are actually leaking memory regions (with default 
io.netty.allocator.useCacheForAllThread) that need the holding thread to die in 
order to make them available to other threads again
   
   I assumed memory released into tiny/small/medium cache would be leaked if 
holding thread terminates until I looked at the code in PoolThreadCache:
   ```
   /// TODO: In the future when we move to Java9+ we should use 
java.lang.ref.Cleaner.
       @Override
       protected void finalize() throws Throwable {
           try {
               super.finalize();
           } finally {
               free();
           }
       }
   
       /**
        *  Should be called if the Thread that uses this cache is about to 
exist to release resources out of the cache
        */
       void free() {
           // As free() may be called either by the finalizer or by 
FastThreadLocal.onRemoval(...) we need to ensure
           // we only call this one time.
           if (freed.compareAndSet(false, true)) {
               int numFreed = free(tinySubPageDirectCaches) +
                       free(smallSubPageDirectCaches) +
                       free(normalDirectCaches) +
                       free(tinySubPageHeapCaches) +
                       free(smallSubPageHeapCaches) +
                       free(normalHeapCaches);
   
               if (numFreed > 0 && logger.isDebugEnabled()) {
                   logger.debug("Freed {} thread-local buffer(s) from thread: 
{}", numFreed,
                           Thread.currentThread().getName());
               }
   
               if (directArena != null) {
                   directArena.numThreadCaches.getAndDecrement();
               }
   
               if (heapArena != null) {
                   heapArena.numThreadCaches.getAndDecrement();
               }
           }
       }
   ```
   So the cache will be freed and used by other threads when thread dies? If 
so, it seems no any leak problems with this pr, no need with improvements?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to