coderZoe commented on issue #518:
URL: https://github.com/apache/mina-sshd/issues/518#issuecomment-2172226499
well,I have discovered another issue: there is a memory leak when
reconnecting the client. Here is an example:
I register a listener for each session to monitor if the session
disconnects. Once it disconnects, I attempt to reconnect proactively.
```java
this.session.addSessionListener(new SessionListener() {
@Override
public void sessionClosed(Session session) {
synchronized (BaseSshSessionSender.this){
if(BaseSshSessionSender.this.status != SenderStatus.DISCONNECT
&& BaseSshSessionSender.this.status != SenderStatus.DEATH){
BaseSshSessionSender.this.status = SenderStatus.DISCONNECT;
BaseSshSessionSender.this.reConnect(true);
}
}
}
});
```
```java
protected void reConnect(boolean immediately){
this.scheduledExecutorService.schedule(this::connect,
immediately?0L:metaData.getReconnectInterval().getSeconds(), TimeUnit.SECONDS);
}
public void connect() {
try {
if(this.status != SenderStatus.DEATH){
this.doConnect();
}
} catch (Exception e) {
this.status = SenderStatus.DISCONNECT;
this.reConnect(false);
}
}
```
Assuming each client currently uses `cpu*2` threads, a reconnection will use
a new client, which creates another `cpu*2` threads. However, the previously
created `cpu*2` threads for the client are not reclaimed. This means each
reconnection increases the number of threads by `cpu*2`, causing the total
number of threads to keep growing.
I tested this with Jprofiler, and found that at service startup, there were
around 1200 sshd threads, but after about 5 minutes, there were nearly 2000
threads (because I intentionally connected some clients to unreachable
networks, triggering reconnections). This situation clearly leads to a memory
leak. How can this be resolved?
Thank you for your assistance.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]