Memory leak:HttpBindBody takes up a lot of memory, causing OOME error

Maybe we don’t need to use the ThreadLocal object here,It causes oome errors.

Each time HttpBindBody is created, a ThreadLocal object will be created to store XMPPPacketReader. However, the XMPPPacketReader object is not deleted from ThreadLocal after use, that is, it is still stored in thread.

When threads are reused, the threadLocations table in the thread object becomes larger and larger.
Here is a sequence diagram and code snippet:

In HttpBindBody.java

private ThreadLocal<XMPPPacketReader> localReader = new ThreadLocal<>();
    private XMPPPacketReader getPacketReader()
    {
        // Reader is associated with a new XMPPPacketReader
        XMPPPacketReader reader = localReader.get();
        if ( reader == null )
        {
            reader = new XMPPPacketReader();
            reader.setXPPFactory( factory );
            localReader.set( reader );
        }
        return reader;
    }

I have dump heap and open it, find some message:

Thanks for this analysis. Are you able to submit a GitHub Pull Request to resolve this issue?

The typical use for ThreadLocal is to make it a private final static variable.

As in the classes ConnectionHandler and HttpSession.

I guess it is solved by simply making it static.

I didn’t submit GitHub’s PullRequest, because I’m not familiar with Ppenfire. I don’t know if it would be unexpected to change this variable to static decoration, but I don’t think the ThreadLocal object should even be used in this place.

I don’t think the ThreadLocal object should even be used in this place.