[ 
https://issues.apache.org/jira/browse/LOG4J2-1387?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15288824#comment-15288824
 ] 

Remko Popma edited comment on LOG4J2-1387 at 5/18/16 2:56 PM:
--------------------------------------------------------------

I am not 100% sure but it looks like the anonymous inner class in 
DefaultShutdownCallbackRegistry has a reference to its outer class which 
somehow causes this leak...

I changed the anonymous inner class to a static inner class. Can you try if the 
version in master still has the issue? (Build with {{mvn clean install}}.)


was (Author: [email protected]):
I am not 100% sure but it looks like the anonymous inner class in 
DefaultShutdownCallbackRegistry has a reference to its outer class which 
somehow causes this leak...

I will try this patch
{code}
Index: 
log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- 
log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java
    (revision )
+++ 
log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java
    (revision )
@@ -78,33 +78,43 @@
         }
     }
 
-    @Override
-    public Cancellable addShutdownCallback(final Runnable callback) {
-        if (isStarted()) {
-            final Cancellable receipt = new Cancellable() {
+    private static class MyCancellable implements Cancellable {
-                // use a reference to prevent memory leaks
+        // use a reference to prevent memory leaks
-                private final Reference<Runnable> hook = new 
SoftReference<>(callback);
+        private final Reference<Runnable> hook;
+        private Collection<Cancellable> registered;
 
+        MyCancellable(final Runnable callback, final Collection<Cancellable> 
registered) {
+            this.registered = registered;
+            hook = new SoftReference<>(callback);
+        }
+
+
-                @Override
-                public void cancel() {
-                    hook.clear();
+        @Override
+        public void cancel() {
+            hook.clear();
-                    hooks.remove(this);
+            registered.remove(this);
+            registered = null;
-                }
+        }
 
-                @Override
-                public void run() {
-                    final Runnable runnableHook = this.hook.get();
-                    if (runnableHook != null) {
-                        runnableHook.run();
-                        this.hook.clear();
-                    }
-                }
+        @Override
+        public void run() {
+            final Runnable runnableHook = this.hook.get();
+            if (runnableHook != null) {
+                runnableHook.run();
+                this.hook.clear();
+            }
+        }
 
-                @Override
-                public String toString() {
-                    return String.valueOf(hook.get());
-                }
+        @Override
+        public String toString() {
+            return String.valueOf(hook.get());
+        }
-            };
+    }
+
+    @Override
+    public Cancellable addShutdownCallback(final Runnable callback) {
+        if (isStarted()) {
+            final Cancellable receipt = new MyCancellable(callback, hooks);
             hooks.add(receipt);
             return receipt;
         }

{code}

> Memory leak related to shutdown hook
> ------------------------------------
>
>                 Key: LOG4J2-1387
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1387
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.5
>            Reporter: Yang ZHONG
>         Attachments: HeapDump.png
>
>
> [~yang_zhong] reported this issue with the shutdown hook on LOG4J2-868 
> (cannot raise a new issue because this is disabled to defend against an 
> Denial of Service attack):
> {quote}
> It's leaking memory in log4J 2.5 (AsyncLoggerContext) @ JDK 8u92.
> Total 2G memory used by 2 AsyncLoggerContext, each references 1G 
> DefaultShutdownCallBackRegistry$1
> which references 4G DefaultShutdownCallBackRegistry
> which references 4G CopyOnWriteArrayList
> which references many many many DefaultShutdownCallBackRegistry$1
> How to turn it off or what's the work-around please?
> {quote}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to