Allow the garbage collection of all object instances of a closed and unreferenced connection

Description

All instances related to an closed and unreferenced connection must be able to get garbage collected. This often failed in the past, causing a memory leak. The problem was/is especially severe on Android with aSmack, where we have frequent reconnects, because of connectivity changes (GSM<->WiFi) and limited memory. The root cause remains unknown atm, it's even possible that this is caused by many bugs/design flaws in Smack.

Possible/Identified causes are:

Issues regarding memory leaks

The Managers need to be redesigned, similar to the way of the attached patch. We need to design a common way how managers are created that allows the garbage collection of all their resources.

The codebase contains some flawed/wrong approaches to fix this. For example PEPManager.java:156 tries it with finalize. But it's a function without effect, because if finalize() if ever called, the local inner class had been already gc'd (Post).

Suggested solution:
1. Managers must not have a public constructor, just a `getInstance(Connection)` method. (We may have to exclude SDM from this constraint, but hopefully not).
2. Managers may always be created even if their functionality is not used. They may no get enabled though.
3. Use WeakHashMap for instances
4. If a Connection instance is used by the manager, make it a weak reference
5. Since Managers may always created, they must come with a enable() and disable() method

An ideal candidate that acts as example Manager that follows the 'new' approach would be the EntityTime Manager ().

To sum up, we have

  • App → Connection: strong reference

  • static manager instances → Connection: weak reference

  • static manager instances → Manager: strong reference

  • Manager → Connection: weak reference

Environment

None

Attachments

1

Activity

Show:

Florian Schmaus October 18, 2014 at 8:20 AM

While it's hard to determine for sure, but I believe most issues regarding memory leaks and garbage collection are now solved in Smack 4.1. If a new issue appears, we can re-open this.

Florian Schmaus March 22, 2013 at 5:45 PM

is about leaking threads, is about leaking memory.

Florian Schmaus March 20, 2013 at 4:43 PM

is a very localized try to fix SMACK-383. It also uses a weak reference for the map's key, which is IMHO not need and also error prone.

Florian Schmaus March 20, 2013 at 4:22 PM

Keep the state after a disconnect is closely related to the clean garbage collection of a unreferenced and closed connection.

Florian Schmaus February 28, 2013 at 5:36 PM

I've scheduled this for a 4.0 release. This issue will be solved and I also have some ideas how, but not with the proposed patches. I think there is a more cleaner way.

Fixed

Details

Assignee

Reporter

Ignite Forum URL

Components

Fix versions

Affects versions

Priority

Created June 6, 2012 at 9:00 AM
Updated October 28, 2020 at 1:28 PM
Resolved October 18, 2014 at 8:20 AM