I got some memoy leak in smack 4.1.0 when create a new XMPPTCPConnection

hi,everyone

I use smack 4.1.0 in my android project.

In my code,when I connect to server every time, I create a new XMPPTCPConnection,so the last one lost the reference in my code,I should be released by gc.

By when I see the java heap when I re login,I saw some resources (PingManager,etc) belonging to the last XMPPTCPConnection is still in heap.

So I track the gc root and I find the reason:

1.WeakHashMap “INSTANCES” in Roster has a weak ref to XMPPTCPConnection and Roster,so any strong ref to XMPPTCPConnection will keep the key-value in the WeakHashMap.

2.a Roster instance has a filed “entries” ,which is a strong ref to RosterEntry

3.a RosterEntry has a filed “connection”,which is strong ref to XMPPTCPConnection.

So,the XMPPTCPConnection can’t be released because of RosterEntry in 3,the RosterEntry can’t be released because of Roster in 2,the Roster can’t be released because of XMPPTCPConnection in 1.

It’s a loop.

It’s the analysis above right?

How can I avoid the problem?

thanks!

Thumbs up for the detailed analysis. It appears that Java developers knowing how to read the heap are rare these days. I’ve created SMACK-659 and a fix in the queue.

That said, you should really consider re-using the XMPP(TCP)Connection instance as long as possible.

Thanks!

I try to reuse the XMPP(TCP)Connection!