Ke Xu created SSHD-646:
--------------------------

             Summary: ThreadGroup created in ThreadUtils is not destroyed and 
causes memory leak
                 Key: SSHD-646
                 URL: https://issues.apache.org/jira/browse/SSHD-646
             Project: MINA SSHD
          Issue Type: Bug
    Affects Versions: 1.0.0
         Environment: linux
            Reporter: Ke Xu


===============
Problem description
===============
In our application, we found that more than 250M memory were occupied by around 
700,000 ThreadGroup objects (whose names are SshClient-nio2, timer, 
ClientInputStreamPump). The Threads contained in these ThreadGroups and their 
sub groups were all null.

===============
Analysis
===============
In ThreadUtils class there is an inner class named SshdThreadFactory. Inside 
this class, it will create a ThreadGroup and attach it to the parent 
Threadgroup: 
group = new ThreadGroup(parentGroup, "sshd-" + effectiveName + "-group");

However, the destroy() method of the ThreadGroup object is never explicitely 
called when relevent resources are closed. 

According to ThreadGroup API, when a ThreadGroup is created with parent 
ThreadGroup specified, a reference to this child ThreadGroup is created in the 
parent. The destroy() method is the only way to make the parent to remove this 
reference. 

Thus in case the parent ThreadGroup is main, and there is a lot of ThreadGroup 
created, the ThreadGoup objects are not collected by the system until program 
exits. It will eventually occupy a lot of memory.

===============
Suggested solution
===============
A suggested solution is to mark the ThreadGroup object as daemon after it is 
created in SshdThreadFactory, e.g.:

            group = new ThreadGroup(parentGroup, "sshd-" + effectiveName + 
"-group");
            group.setDaemon(true); 

According to API, when a ThreadGroup is marked as daemon, it is automatically 
destroyed when its last thread is stopped or its last thread group is destroyed.



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

Reply via email to