Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leaks using Executors.newFixedThreadPool()

I am using Spring3.1 on a standalone env.

(this problem not necessary related to Spring. it's behaving the same also on a standalone env.)

I have implements a listener which receives messages from Topic. the messages rate is very very high (talking about 20/30 m/s).

some messages can take more processing time then other.

The listener works with the same instance which means that if one message being processed too long it hits our performances pretty much.

We thought about doing our own pool of objects instead of using the same listener instance but then I found out the Executors(java.util.concurrent.Executors).

So for each message received a different thread will be allocated to it. this will make sure our listener instance will be free to process messages parallel.

private ExecutorService  threadPool = Executors.newFixedThreadPool(100);
    @Override
    public void onMessage(final Message msg)
    {
        Runnable t = new Runnable()
        {
            public void run()
            {
                onSessionMessage(msg);
                log.trace("AbstractSessionBean, received messge");
            }
        };
        threadPool.execute(t);
    }

That seems to solve our performance issue. but after monitoring the application with jconsole we facing now huge memory leaks.

The heap memory usage being increased significantly in time.

So I tried to "play" a bit with the FixedThreadPool size number. still having huge memory usage:

enter image description here

Any idea how can I solve this? any other ideas to solve my key problem?

jconsole after performing GB

jconsole overall view

After running heap dump I got two problem suspects:

Head Dump

thanks, ray.

like image 399
rayman Avatar asked Dec 17 '12 08:12

rayman


1 Answers

I believe the issue you encountered is the threadPool not releasing its resources. You need to call threadPool.shutdown() after you are finished submitting or executing. This will wait until the tasks have completed before terminating threads which can then be garbage collected.

From official Java api website:

"An unused ExecutorService should be shut down to allow reclamation of its resources." https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#shutdown()

Alternatively you can use a newCachedThreadPool() which "Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available" see https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html

When I encountered this problem, I went with the newFixedThreadPool() and shutdown option.

like image 67
Enda Avatar answered Oct 02 '22 17:10

Enda