From f4f7eca7ca34b3017684817d6d1b50dab87a7c34 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 21 May 2014 13:02:32 +0200 Subject: [PATCH] Shutdown listenerExecutor to prevent the Thread from leaking listenerExecutor needs to get shutdown once it is no longer required. SMACK-567 --- .../org/jivesoftware/smack/XMPPConnection.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index 3269a8f47..fb6c3b608 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -1318,4 +1318,19 @@ public abstract class XMPPConnection { public FromMode getFromMode() { return this.fromMode; } + + @Override + protected void finalize() throws Throwable { + try { + // It's usually not a good idea to rely on finalize. But this is the easiest way to + // avoid the "Smack Listener Processor" leaking. The thread(s) of the executor have a + // reference to their ExecutorService which prevents the ExecutorService from being + // gc'ed. It is possible that the XMPPConnection instance is gc'ed while the + // listenerExecutor ExecutorService call not be gc'ed until it got shut down. + listenerExecutor.shutdownNow(); + } + finally { + super.finalize(); + } + } }