You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Yongxing Wang <yw...@sharpcast.com> on 2009/05/14 08:23:30 UTC

memory leak? -- help needed

Gurus,

 

I need your help to identify whether MINA has memory leak. I am little
lost as what is happening. 

 

We are currently using MINA 2.0.0-M5. The machine has 16G memory and I
allocate 12G for the process. After running for a day under fairly heavy
load, it runs into OutOfMemory(top shows that the process takes 75% of
memory, which is exactly right). 

 

So I took it out of load balancer, but I did NOT take down the process.
Three hours later (GC claimed back some memory, it was back to 68%
instead of 75%), I took a jmap against the process. It generated more
than 10G of dump data. I examined the result and found out that are tons
(495,968 in total) of the DefaultWriteRequest (see below) hanging
around. Though it is small, the message contained inside is huge.

 

There is no network traffic to the process after cutting it off the load
balancer and it has been long enough, any idea why those objects are
still hanging around?

 

 

Your insight is highly appreciated!

 

Yong

 

 

------------------------------------------------------------------------
-----------------------------------------------------------------

 


instance of
org.apache.mina.core.write.DefaultWriteRequest@0x2aab263fd080 (40 bytes)


Class:


class org.apache.mina.core.write.DefaultWriteRequest
<http://localhost:33333/class/0x2aaaae9afd70>  


Instance data members:


destination (L) : <null>
future (L) :
org.apache.mina.core.future.DefaultWriteFuture@0x2aab263ffe60 (61 bytes)
<http://localhost:33333/object/0x2aab263ffe60>  
message (L) : [B@0x2aab263fd0a8 (11690 bytes)
<http://localhost:33333/object/0x2aab263fd0a8>  


References to this object:


org.apache.mina.filter.codec.ProtocolCodecFilter$MessageWriteRequest@0x2
aab263fd068 (24 bytes) <http://localhost:33333/object/0x2aab263fd068>  :
field parentRequest


Re: memory leak? -- help needed

Posted by David Rosenstrauch <da...@darose.net>.
Emmanuel Lecharny wrote:
> In any case, the 'finalize' method is not one of my favorite method... Not
> only it has a very specific usage, but there is no guarantee whatsoever to
> be called by the GC. I will check if it's necessary to have it.

Good idea.  Not only is it not guaranteed to get called, but finalize 
can cause some very unexpected memory behavior.  See:

http://www.fasterj.com/articles/finalizer1.shtml
http://www.fasterj.com/articles/finalizer2.shtml

HTH,

DR

RE: memory leak? -- help needed

Posted by Yongxing Wang <yw...@sharpcast.com>.
I think my problem has something to do with it. But to be honest, I am not totally sure that is *the* *only* problem (with 10G of dump data, it is just hard to find useful things even with all kinds of tools). I manually patched the code and I will be able to put the code under stress test (the same level when OutOfMemory happens to me last time) next Monday and see how it behaves.

I will keep you posted.

Thanks,
Yong

-----Original Message-----
From: elecharny@gmail.com [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
Sent: Friday, May 15, 2009 9:33 AM
To: users@mina.apache.org
Subject: Re: memory leak? -- help needed

FYI, this finalize method was added on revision 190270, three years and 11
months ago ...

http://svn.apache.org/viewvc/directory/network/trunk/src/java/org/apache/mina/common/AbstractIoFilterChain.java?r1=190270&r2=190269&pathrev=190270

So it has been present in the code base before the first version was
released... I would be very surprised if nobody has found this problem since
then. There must but something else going wrong...

I'm not saying that you are wrong, I don't like this method either, but I
want to be sure that we are focusing on the real problem here.

On Fri, May 15, 2009 at 6:21 PM, Emmanuel Lecharny <el...@apache.org>wrote:

> Thanks for the detailed explaination. I will spend some time tonight to
> build a sample application exhibiting the behaviour you are exposing (unless
> you have already a portion of code for that !).
>
> In any case, the 'finalize' method is not one of my favorite method... Not
> only it has a very specific usage, but there is no guarantee whatsoever to
> be called by the GC. I will check if it's necessary to have it.
>
>
> On Fri, May 15, 2009 at 6:14 PM, Yongxing Wang <yw...@sharpcast.com>wrote:
>
>> Emmanuel,
>>
>>
>> When client close connection, session should be discarded and it is kind
>> of so. The only thing, I think, is because that the "finalize" method on
>> DefaultIoFilterChain. Because it has such method, JVM can't claim the memory
>> back right away because inside of the finalize code, it is impossible the
>> very same object could be resurrected (because we users provide the
>> implementation), so GC has to treat them quite differently (possibly go over
>> multiple GC cycles before fully discard it). That's why the chain is still
>> referenced by the Finalizer and so the session.
>>
>> In theory, those objects should be eventually recycled. But the timing is
>> totally un-deterministic and they can stay there for unnecessarily long.
>> This is even true to Session objects. They are long live objects and most
>> likely stay in PermGen, where GC recycles much less frequently.
>>
>> You can do a simple test. Use a client/server example in MINA code, start
>> the server, start one client and let the client do some work and then close
>> the client. Wait for 10 minutes, check the memory map of the server. Chances
>> are, you can still find that Session object. If you trace it to "GC rootset
>> exclude weak references", you will find that the session is referenced by
>> the chain, which is referenced by Finalizer.
>>
>> Now remove the "finalize" method in the chain and do the same test, you
>> will find a different result.
>>
>> Regardless of whether that is the true source of my memory leak, hopefully
>> you can recognize the "finalize" method is evil. In the case of the filter
>> chain, I don't see any reason why it is adopted. Getting rid of that would
>> be a very good idea.
>>
>> Thanks,
>> Yong
>>
>> Here is how my chain is initialized.
>> ----------------------------------------------------------------------
>>
>> DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
>> chain.addLast("codec", new ProtocolCodecFilter(new
>>  ScMessageProtocolCodecFactory()));
>>
>> chain.addLast("logger", new LoggingFilter());
>> chain.addLast("monitor", new ProtocolMonitorFilter());
>>
>> ExecutorService threadPool = new OrderedThreadPoolExecutor(4);
>> chain.addLast("executor", new ExecutorFilter(threadPool));
>> acceptor.setHandler(new CentralDispatchHandler());
>>
>>
>> -----Original Message-----
>> From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of
>> Emmanuel Lecharny
>> Sent: Friday, May 15, 2009 1:17 AM
>> To: users@mina.apache.org
>> Subject: Re: memory leak? -- help needed
>>
>> Hi,
>>
>> this is very strange... If the client close the connection before the
>> message is sent to him, then the session should be discarded.
>>
>> Could you provide the code which initialize the chain you are using ?
>>
>> Also could you run your test with the latest version (ie, the trunk)? We
>> have fixed some bad issues lately about the way messages are handled.
>>
>> Thanks !
>>
>>
>> Yongxing Wang wrote:
>>
>> > In sessionClosed() method in DefaultIoFilterChain, it will do:
>> >
>> > s.getWriteRequestQueue().dispose(session);
>> >
>> >
>> > There are only two classes that implements WriteRequestQueue, one is
>> CloseAwareWriteQueue and another is DefaultWriteRequestQueue.
>> CloseAwareWriteQueue is just a wrapper on top of DefaultWriteRequestQueue
>> where the dispose() method does nothing.
>> >
>> >   public void dispose(IoSession session) {
>> >   }
>> >
>> >
>> > So I have a theory for the mem leak:
>> >
>> > 1: Client request something,
>> > 2: Server fetch the data and call session.write(), the data got queued
>> in the wirteRequestQueue.
>> > 3: Before the data got actually flushed, the client drops the connection
>> > 4: MINA fires sessionClosed() as expect, but the dispose method didn't
>> actually clear the queue.
>> > 5: Because of the session still got referenced by the Finalizer, the
>> huge amount of data just hang around.
>> >
>> >
>> > Am I right that the dispose() method should clear the queue?
>> >
>> > Thanks,
>> > Yong
>> >
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: Yongxing Wang
>> > Sent: Thursday, May 14, 2009 9:31 AM
>> > To: 'users@mina.apache.org'
>> > Subject: RE: memory leak? -- help needed
>> >
>> > Yes, I will definitely run it against one of those tools (my Jprofile
>> evaluation expired, need to work it out).
>> >
>> > I guess last night my brain was fried and I didn't provide enough
>> information. The dump analysis told me that there are still more than 7K
>> NioSocketSession (referenced by java.lang.Finalizer) hanging around three
>> hours after I the process out of LB. This probably related to one of my
>> earlier post:
>> >
>> > "Looking at the source code, it is caused by DefaultIoFilterChain
>> (because it has a finalize method)." -- the chain holds a reference to the
>> session.
>> >
>> > I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of
>> dump data, the query never returns.
>> >
>> > I will continue to do more analysis. At the mean time, is there a reason
>> that you have to use "finalize" method in the DefaultIoFilterChain?
>> >
>> > Yong
>> >
>> >
>> > -----Original Message-----
>> > From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of
>> Emmanuel Lecharny
>> > Sent: Wednesday, May 13, 2009 11:47 PM
>> > To: users@mina.apache.org
>> > Subject: Re: memory leak? -- help needed
>> >
>> > Yongxing Wang wrote:
>> >
>> >> Gurus,
>> >>
>> >>
>> > hi,
>> >
>> > you should use a tool to determine if the memory leak you are
>> > experiencing is part of your own code. You have plenty of them out there
>> > : JProfiler, OptimizeIt, YourKit, ...
>> >
>> > This will give you some insight about the part of the code where this
>> > memory leak is likely to be created.
>> >
>> > There is not much we can do with the provided information atm...
>> >
>> >
>>
>>
>> --
>> --
>> cordialement, regards,
>> Emmanuel Lécharny
>> www.iktek.com
>> directory.apache.org
>>
>>
>>
>
>
> --
> Regards,
> Cordialement,
>
> Emmanuel Lécharny
> www.iktek.com
>



-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

RE: memory leak? -- help needed

Posted by Yongxing Wang <yw...@sharpcast.com>.
https://issues.apache.org/jira/browse/DIRMINA-716 is created for this issue.

Thanks!

Yong

-----Original Message-----
From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
Sent: Wednesday, May 27, 2009 1:09 AM
To: users@mina.apache.org
Subject: Re: memory leak? -- help needed

Yongxing Wang wrote:
> Sorry to get to you guys a little late on this issue, as it is not always easy to reproduce the problem. 
>
> During this extensive performance testing and close monitoring, one thing I can tell for sure is that under heavy load, the memory pattern looks so much nicer if we get rid of the "finalize" method. It contributes to the better overall system load.
>
> I strongly recommend getting rid of the "finalize" method in AbstractIoFilterChain.java.
>   
Using finalize is most certainly an error. It's certainly not good to 
free part of memory which are not allocated by a native method. I agree 
that we should remove it.

Can you create a JIRA for tat please ?

Thanks !

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: memory leak? -- help needed

Posted by Emmanuel Lecharny <el...@apache.org>.
Yongxing Wang wrote:
> Sorry to get to you guys a little late on this issue, as it is not always easy to reproduce the problem. 
>
> During this extensive performance testing and close monitoring, one thing I can tell for sure is that under heavy load, the memory pattern looks so much nicer if we get rid of the "finalize" method. It contributes to the better overall system load.
>
> I strongly recommend getting rid of the "finalize" method in AbstractIoFilterChain.java.
>   
Using finalize is most certainly an error. It's certainly not good to 
free part of memory which are not allocated by a native method. I agree 
that we should remove it.

Can you create a JIRA for tat please ?

Thanks !

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



RE: memory leak? -- help needed

Posted by Yongxing Wang <yw...@sharpcast.com>.
Sorry to get to you guys a little late on this issue, as it is not always easy to reproduce the problem. 

During this extensive performance testing and close monitoring, one thing I can tell for sure is that under heavy load, the memory pattern looks so much nicer if we get rid of the "finalize" method. It contributes to the better overall system load.

I strongly recommend getting rid of the "finalize" method in AbstractIoFilterChain.java.

Thanks,
Yong



-----Original Message-----
From: Yongxing Wang 
Sent: Friday, May 15, 2009 9:41 AM
To: 'users@mina.apache.org'
Subject: RE: memory leak? -- help needed

I think my problem has something to do with it. But to be honest, I am not totally sure that is *the* *only* problem (with 10G of dump data, it is just hard to find useful things even with all kinds of tools). I manually patched the code and I will be able to put the code under stress test (the same level when OutOfMemory happens to me last time) next Monday and see how it behaves.

I will keep you posted.

Thanks,
Yong

-----Original Message-----
From: elecharny@gmail.com [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
Sent: Friday, May 15, 2009 9:33 AM
To: users@mina.apache.org
Subject: Re: memory leak? -- help needed

FYI, this finalize method was added on revision 190270, three years and 11
months ago ...

http://svn.apache.org/viewvc/directory/network/trunk/src/java/org/apache/mina/common/AbstractIoFilterChain.java?r1=190270&r2=190269&pathrev=190270

So it has been present in the code base before the first version was
released... I would be very surprised if nobody has found this problem since
then. There must but something else going wrong...

I'm not saying that you are wrong, I don't like this method either, but I
want to be sure that we are focusing on the real problem here.

On Fri, May 15, 2009 at 6:21 PM, Emmanuel Lecharny <el...@apache.org>wrote:

> Thanks for the detailed explaination. I will spend some time tonight to
> build a sample application exhibiting the behaviour you are exposing (unless
> you have already a portion of code for that !).
>
> In any case, the 'finalize' method is not one of my favorite method... Not
> only it has a very specific usage, but there is no guarantee whatsoever to
> be called by the GC. I will check if it's necessary to have it.
>
>
> On Fri, May 15, 2009 at 6:14 PM, Yongxing Wang <yw...@sharpcast.com>wrote:
>
>> Emmanuel,
>>
>>
>> When client close connection, session should be discarded and it is kind
>> of so. The only thing, I think, is because that the "finalize" method on
>> DefaultIoFilterChain. Because it has such method, JVM can't claim the memory
>> back right away because inside of the finalize code, it is impossible the
>> very same object could be resurrected (because we users provide the
>> implementation), so GC has to treat them quite differently (possibly go over
>> multiple GC cycles before fully discard it). That's why the chain is still
>> referenced by the Finalizer and so the session.
>>
>> In theory, those objects should be eventually recycled. But the timing is
>> totally un-deterministic and they can stay there for unnecessarily long.
>> This is even true to Session objects. They are long live objects and most
>> likely stay in PermGen, where GC recycles much less frequently.
>>
>> You can do a simple test. Use a client/server example in MINA code, start
>> the server, start one client and let the client do some work and then close
>> the client. Wait for 10 minutes, check the memory map of the server. Chances
>> are, you can still find that Session object. If you trace it to "GC rootset
>> exclude weak references", you will find that the session is referenced by
>> the chain, which is referenced by Finalizer.
>>
>> Now remove the "finalize" method in the chain and do the same test, you
>> will find a different result.
>>
>> Regardless of whether that is the true source of my memory leak, hopefully
>> you can recognize the "finalize" method is evil. In the case of the filter
>> chain, I don't see any reason why it is adopted. Getting rid of that would
>> be a very good idea.
>>
>> Thanks,
>> Yong
>>
>> Here is how my chain is initialized.
>> ----------------------------------------------------------------------
>>
>> DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
>> chain.addLast("codec", new ProtocolCodecFilter(new
>>  ScMessageProtocolCodecFactory()));
>>
>> chain.addLast("logger", new LoggingFilter());
>> chain.addLast("monitor", new ProtocolMonitorFilter());
>>
>> ExecutorService threadPool = new OrderedThreadPoolExecutor(4);
>> chain.addLast("executor", new ExecutorFilter(threadPool));
>> acceptor.setHandler(new CentralDispatchHandler());
>>
>>
>> -----Original Message-----
>> From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of
>> Emmanuel Lecharny
>> Sent: Friday, May 15, 2009 1:17 AM
>> To: users@mina.apache.org
>> Subject: Re: memory leak? -- help needed
>>
>> Hi,
>>
>> this is very strange... If the client close the connection before the
>> message is sent to him, then the session should be discarded.
>>
>> Could you provide the code which initialize the chain you are using ?
>>
>> Also could you run your test with the latest version (ie, the trunk)? We
>> have fixed some bad issues lately about the way messages are handled.
>>
>> Thanks !
>>
>>
>> Yongxing Wang wrote:
>>
>> > In sessionClosed() method in DefaultIoFilterChain, it will do:
>> >
>> > s.getWriteRequestQueue().dispose(session);
>> >
>> >
>> > There are only two classes that implements WriteRequestQueue, one is
>> CloseAwareWriteQueue and another is DefaultWriteRequestQueue.
>> CloseAwareWriteQueue is just a wrapper on top of DefaultWriteRequestQueue
>> where the dispose() method does nothing.
>> >
>> >   public void dispose(IoSession session) {
>> >   }
>> >
>> >
>> > So I have a theory for the mem leak:
>> >
>> > 1: Client request something,
>> > 2: Server fetch the data and call session.write(), the data got queued
>> in the wirteRequestQueue.
>> > 3: Before the data got actually flushed, the client drops the connection
>> > 4: MINA fires sessionClosed() as expect, but the dispose method didn't
>> actually clear the queue.
>> > 5: Because of the session still got referenced by the Finalizer, the
>> huge amount of data just hang around.
>> >
>> >
>> > Am I right that the dispose() method should clear the queue?
>> >
>> > Thanks,
>> > Yong
>> >
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: Yongxing Wang
>> > Sent: Thursday, May 14, 2009 9:31 AM
>> > To: 'users@mina.apache.org'
>> > Subject: RE: memory leak? -- help needed
>> >
>> > Yes, I will definitely run it against one of those tools (my Jprofile
>> evaluation expired, need to work it out).
>> >
>> > I guess last night my brain was fried and I didn't provide enough
>> information. The dump analysis told me that there are still more than 7K
>> NioSocketSession (referenced by java.lang.Finalizer) hanging around three
>> hours after I the process out of LB. This probably related to one of my
>> earlier post:
>> >
>> > "Looking at the source code, it is caused by DefaultIoFilterChain
>> (because it has a finalize method)." -- the chain holds a reference to the
>> session.
>> >
>> > I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of
>> dump data, the query never returns.
>> >
>> > I will continue to do more analysis. At the mean time, is there a reason
>> that you have to use "finalize" method in the DefaultIoFilterChain?
>> >
>> > Yong
>> >
>> >
>> > -----Original Message-----
>> > From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of
>> Emmanuel Lecharny
>> > Sent: Wednesday, May 13, 2009 11:47 PM
>> > To: users@mina.apache.org
>> > Subject: Re: memory leak? -- help needed
>> >
>> > Yongxing Wang wrote:
>> >
>> >> Gurus,
>> >>
>> >>
>> > hi,
>> >
>> > you should use a tool to determine if the memory leak you are
>> > experiencing is part of your own code. You have plenty of them out there
>> > : JProfiler, OptimizeIt, YourKit, ...
>> >
>> > This will give you some insight about the part of the code where this
>> > memory leak is likely to be created.
>> >
>> > There is not much we can do with the provided information atm...
>> >
>> >
>>
>>
>> --
>> --
>> cordialement, regards,
>> Emmanuel Lécharny
>> www.iktek.com
>> directory.apache.org
>>
>>
>>
>
>
> --
> Regards,
> Cordialement,
>
> Emmanuel Lécharny
> www.iktek.com
>



-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

Re: memory leak? -- help needed

Posted by Emmanuel Lecharny <el...@apache.org>.
FYI, this finalize method was added on revision 190270, three years and 11
months ago ...

http://svn.apache.org/viewvc/directory/network/trunk/src/java/org/apache/mina/common/AbstractIoFilterChain.java?r1=190270&r2=190269&pathrev=190270

So it has been present in the code base before the first version was
released... I would be very surprised if nobody has found this problem since
then. There must but something else going wrong...

I'm not saying that you are wrong, I don't like this method either, but I
want to be sure that we are focusing on the real problem here.

On Fri, May 15, 2009 at 6:21 PM, Emmanuel Lecharny <el...@apache.org>wrote:

> Thanks for the detailed explaination. I will spend some time tonight to
> build a sample application exhibiting the behaviour you are exposing (unless
> you have already a portion of code for that !).
>
> In any case, the 'finalize' method is not one of my favorite method... Not
> only it has a very specific usage, but there is no guarantee whatsoever to
> be called by the GC. I will check if it's necessary to have it.
>
>
> On Fri, May 15, 2009 at 6:14 PM, Yongxing Wang <yw...@sharpcast.com>wrote:
>
>> Emmanuel,
>>
>>
>> When client close connection, session should be discarded and it is kind
>> of so. The only thing, I think, is because that the "finalize" method on
>> DefaultIoFilterChain. Because it has such method, JVM can't claim the memory
>> back right away because inside of the finalize code, it is impossible the
>> very same object could be resurrected (because we users provide the
>> implementation), so GC has to treat them quite differently (possibly go over
>> multiple GC cycles before fully discard it). That's why the chain is still
>> referenced by the Finalizer and so the session.
>>
>> In theory, those objects should be eventually recycled. But the timing is
>> totally un-deterministic and they can stay there for unnecessarily long.
>> This is even true to Session objects. They are long live objects and most
>> likely stay in PermGen, where GC recycles much less frequently.
>>
>> You can do a simple test. Use a client/server example in MINA code, start
>> the server, start one client and let the client do some work and then close
>> the client. Wait for 10 minutes, check the memory map of the server. Chances
>> are, you can still find that Session object. If you trace it to "GC rootset
>> exclude weak references", you will find that the session is referenced by
>> the chain, which is referenced by Finalizer.
>>
>> Now remove the "finalize" method in the chain and do the same test, you
>> will find a different result.
>>
>> Regardless of whether that is the true source of my memory leak, hopefully
>> you can recognize the "finalize" method is evil. In the case of the filter
>> chain, I don't see any reason why it is adopted. Getting rid of that would
>> be a very good idea.
>>
>> Thanks,
>> Yong
>>
>> Here is how my chain is initialized.
>> ----------------------------------------------------------------------
>>
>> DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
>> chain.addLast("codec", new ProtocolCodecFilter(new
>>  ScMessageProtocolCodecFactory()));
>>
>> chain.addLast("logger", new LoggingFilter());
>> chain.addLast("monitor", new ProtocolMonitorFilter());
>>
>> ExecutorService threadPool = new OrderedThreadPoolExecutor(4);
>> chain.addLast("executor", new ExecutorFilter(threadPool));
>> acceptor.setHandler(new CentralDispatchHandler());
>>
>>
>> -----Original Message-----
>> From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of
>> Emmanuel Lecharny
>> Sent: Friday, May 15, 2009 1:17 AM
>> To: users@mina.apache.org
>> Subject: Re: memory leak? -- help needed
>>
>> Hi,
>>
>> this is very strange... If the client close the connection before the
>> message is sent to him, then the session should be discarded.
>>
>> Could you provide the code which initialize the chain you are using ?
>>
>> Also could you run your test with the latest version (ie, the trunk)? We
>> have fixed some bad issues lately about the way messages are handled.
>>
>> Thanks !
>>
>>
>> Yongxing Wang wrote:
>>
>> > In sessionClosed() method in DefaultIoFilterChain, it will do:
>> >
>> > s.getWriteRequestQueue().dispose(session);
>> >
>> >
>> > There are only two classes that implements WriteRequestQueue, one is
>> CloseAwareWriteQueue and another is DefaultWriteRequestQueue.
>> CloseAwareWriteQueue is just a wrapper on top of DefaultWriteRequestQueue
>> where the dispose() method does nothing.
>> >
>> >   public void dispose(IoSession session) {
>> >   }
>> >
>> >
>> > So I have a theory for the mem leak:
>> >
>> > 1: Client request something,
>> > 2: Server fetch the data and call session.write(), the data got queued
>> in the wirteRequestQueue.
>> > 3: Before the data got actually flushed, the client drops the connection
>> > 4: MINA fires sessionClosed() as expect, but the dispose method didn't
>> actually clear the queue.
>> > 5: Because of the session still got referenced by the Finalizer, the
>> huge amount of data just hang around.
>> >
>> >
>> > Am I right that the dispose() method should clear the queue?
>> >
>> > Thanks,
>> > Yong
>> >
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: Yongxing Wang
>> > Sent: Thursday, May 14, 2009 9:31 AM
>> > To: 'users@mina.apache.org'
>> > Subject: RE: memory leak? -- help needed
>> >
>> > Yes, I will definitely run it against one of those tools (my Jprofile
>> evaluation expired, need to work it out).
>> >
>> > I guess last night my brain was fried and I didn't provide enough
>> information. The dump analysis told me that there are still more than 7K
>> NioSocketSession (referenced by java.lang.Finalizer) hanging around three
>> hours after I the process out of LB. This probably related to one of my
>> earlier post:
>> >
>> > "Looking at the source code, it is caused by DefaultIoFilterChain
>> (because it has a finalize method)." -- the chain holds a reference to the
>> session.
>> >
>> > I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of
>> dump data, the query never returns.
>> >
>> > I will continue to do more analysis. At the mean time, is there a reason
>> that you have to use "finalize" method in the DefaultIoFilterChain?
>> >
>> > Yong
>> >
>> >
>> > -----Original Message-----
>> > From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of
>> Emmanuel Lecharny
>> > Sent: Wednesday, May 13, 2009 11:47 PM
>> > To: users@mina.apache.org
>> > Subject: Re: memory leak? -- help needed
>> >
>> > Yongxing Wang wrote:
>> >
>> >> Gurus,
>> >>
>> >>
>> > hi,
>> >
>> > you should use a tool to determine if the memory leak you are
>> > experiencing is part of your own code. You have plenty of them out there
>> > : JProfiler, OptimizeIt, YourKit, ...
>> >
>> > This will give you some insight about the part of the code where this
>> > memory leak is likely to be created.
>> >
>> > There is not much we can do with the provided information atm...
>> >
>> >
>>
>>
>> --
>> --
>> cordialement, regards,
>> Emmanuel Lécharny
>> www.iktek.com
>> directory.apache.org
>>
>>
>>
>
>
> --
> Regards,
> Cordialement,
>
> Emmanuel Lécharny
> www.iktek.com
>



-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

Re: memory leak? -- help needed

Posted by Emmanuel Lecharny <el...@apache.org>.
Thanks for the detailed explaination. I will spend some time tonight to
build a sample application exhibiting the behaviour you are exposing (unless
you have already a portion of code for that !).

In any case, the 'finalize' method is not one of my favorite method... Not
only it has a very specific usage, but there is no guarantee whatsoever to
be called by the GC. I will check if it's necessary to have it.

On Fri, May 15, 2009 at 6:14 PM, Yongxing Wang <yw...@sharpcast.com> wrote:

> Emmanuel,
>
>
> When client close connection, session should be discarded and it is kind of
> so. The only thing, I think, is because that the "finalize" method on
> DefaultIoFilterChain. Because it has such method, JVM can't claim the memory
> back right away because inside of the finalize code, it is impossible the
> very same object could be resurrected (because we users provide the
> implementation), so GC has to treat them quite differently (possibly go over
> multiple GC cycles before fully discard it). That's why the chain is still
> referenced by the Finalizer and so the session.
>
> In theory, those objects should be eventually recycled. But the timing is
> totally un-deterministic and they can stay there for unnecessarily long.
> This is even true to Session objects. They are long live objects and most
> likely stay in PermGen, where GC recycles much less frequently.
>
> You can do a simple test. Use a client/server example in MINA code, start
> the server, start one client and let the client do some work and then close
> the client. Wait for 10 minutes, check the memory map of the server. Chances
> are, you can still find that Session object. If you trace it to "GC rootset
> exclude weak references", you will find that the session is referenced by
> the chain, which is referenced by Finalizer.
>
> Now remove the "finalize" method in the chain and do the same test, you
> will find a different result.
>
> Regardless of whether that is the true source of my memory leak, hopefully
> you can recognize the "finalize" method is evil. In the case of the filter
> chain, I don't see any reason why it is adopted. Getting rid of that would
> be a very good idea.
>
> Thanks,
> Yong
>
> Here is how my chain is initialized.
> ----------------------------------------------------------------------
>
> DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
> chain.addLast("codec", new ProtocolCodecFilter(new
>  ScMessageProtocolCodecFactory()));
>
> chain.addLast("logger", new LoggingFilter());
> chain.addLast("monitor", new ProtocolMonitorFilter());
>
> ExecutorService threadPool = new OrderedThreadPoolExecutor(4);
> chain.addLast("executor", new ExecutorFilter(threadPool));
> acceptor.setHandler(new CentralDispatchHandler());
>
>
> -----Original Message-----
> From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of Emmanuel
> Lecharny
> Sent: Friday, May 15, 2009 1:17 AM
> To: users@mina.apache.org
> Subject: Re: memory leak? -- help needed
>
> Hi,
>
> this is very strange... If the client close the connection before the
> message is sent to him, then the session should be discarded.
>
> Could you provide the code which initialize the chain you are using ?
>
> Also could you run your test with the latest version (ie, the trunk)? We
> have fixed some bad issues lately about the way messages are handled.
>
> Thanks !
>
>
> Yongxing Wang wrote:
>
> > In sessionClosed() method in DefaultIoFilterChain, it will do:
> >
> > s.getWriteRequestQueue().dispose(session);
> >
> >
> > There are only two classes that implements WriteRequestQueue, one is
> CloseAwareWriteQueue and another is DefaultWriteRequestQueue.
> CloseAwareWriteQueue is just a wrapper on top of DefaultWriteRequestQueue
> where the dispose() method does nothing.
> >
> >   public void dispose(IoSession session) {
> >   }
> >
> >
> > So I have a theory for the mem leak:
> >
> > 1: Client request something,
> > 2: Server fetch the data and call session.write(), the data got queued in
> the wirteRequestQueue.
> > 3: Before the data got actually flushed, the client drops the connection
> > 4: MINA fires sessionClosed() as expect, but the dispose method didn't
> actually clear the queue.
> > 5: Because of the session still got referenced by the Finalizer, the huge
> amount of data just hang around.
> >
> >
> > Am I right that the dispose() method should clear the queue?
> >
> > Thanks,
> > Yong
> >
> >
> >
> >
> > -----Original Message-----
> > From: Yongxing Wang
> > Sent: Thursday, May 14, 2009 9:31 AM
> > To: 'users@mina.apache.org'
> > Subject: RE: memory leak? -- help needed
> >
> > Yes, I will definitely run it against one of those tools (my Jprofile
> evaluation expired, need to work it out).
> >
> > I guess last night my brain was fried and I didn't provide enough
> information. The dump analysis told me that there are still more than 7K
> NioSocketSession (referenced by java.lang.Finalizer) hanging around three
> hours after I the process out of LB. This probably related to one of my
> earlier post:
> >
> > "Looking at the source code, it is caused by DefaultIoFilterChain
> (because it has a finalize method)." -- the chain holds a reference to the
> session.
> >
> > I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of
> dump data, the query never returns.
> >
> > I will continue to do more analysis. At the mean time, is there a reason
> that you have to use "finalize" method in the DefaultIoFilterChain?
> >
> > Yong
> >
> >
> > -----Original Message-----
> > From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of
> Emmanuel Lecharny
> > Sent: Wednesday, May 13, 2009 11:47 PM
> > To: users@mina.apache.org
> > Subject: Re: memory leak? -- help needed
> >
> > Yongxing Wang wrote:
> >
> >> Gurus,
> >>
> >>
> > hi,
> >
> > you should use a tool to determine if the memory leak you are
> > experiencing is part of your own code. You have plenty of them out there
> > : JProfiler, OptimizeIt, YourKit, ...
> >
> > This will give you some insight about the part of the code where this
> > memory leak is likely to be created.
> >
> > There is not much we can do with the provided information atm...
> >
> >
>
>
> --
> --
> cordialement, regards,
> Emmanuel Lécharny
> www.iktek.com
> directory.apache.org
>
>
>


-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

RE: memory leak? -- help needed

Posted by Yongxing Wang <yw...@sharpcast.com>.
Emmanuel,


When client close connection, session should be discarded and it is kind of so. The only thing, I think, is because that the "finalize" method on DefaultIoFilterChain. Because it has such method, JVM can't claim the memory back right away because inside of the finalize code, it is impossible the very same object could be resurrected (because we users provide the implementation), so GC has to treat them quite differently (possibly go over multiple GC cycles before fully discard it). That's why the chain is still referenced by the Finalizer and so the session.

In theory, those objects should be eventually recycled. But the timing is totally un-deterministic and they can stay there for unnecessarily long. This is even true to Session objects. They are long live objects and most likely stay in PermGen, where GC recycles much less frequently. 

You can do a simple test. Use a client/server example in MINA code, start the server, start one client and let the client do some work and then close the client. Wait for 10 minutes, check the memory map of the server. Chances are, you can still find that Session object. If you trace it to "GC rootset exclude weak references", you will find that the session is referenced by the chain, which is referenced by Finalizer.

Now remove the "finalize" method in the chain and do the same test, you will find a different result.

Regardless of whether that is the true source of my memory leak, hopefully you can recognize the "finalize" method is evil. In the case of the filter chain, I don't see any reason why it is adopted. Getting rid of that would be a very good idea.

Thanks,
Yong

Here is how my chain is initialized.
----------------------------------------------------------------------

DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("codec", new ProtocolCodecFilter(new            ScMessageProtocolCodecFactory()));
        
chain.addLast("logger", new LoggingFilter());
chain.addLast("monitor", new ProtocolMonitorFilter());
        
ExecutorService threadPool = new OrderedThreadPoolExecutor(4);
chain.addLast("executor", new ExecutorFilter(threadPool));
acceptor.setHandler(new CentralDispatchHandler());


-----Original Message-----
From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
Sent: Friday, May 15, 2009 1:17 AM
To: users@mina.apache.org
Subject: Re: memory leak? -- help needed

Hi,

this is very strange... If the client close the connection before the 
message is sent to him, then the session should be discarded.

Could you provide the code which initialize the chain you are using ?

Also could you run your test with the latest version (ie, the trunk)? We 
have fixed some bad issues lately about the way messages are handled.

Thanks !


Yongxing Wang wrote:

> In sessionClosed() method in DefaultIoFilterChain, it will do:
>
> s.getWriteRequestQueue().dispose(session);
>
>
> There are only two classes that implements WriteRequestQueue, one is CloseAwareWriteQueue and another is DefaultWriteRequestQueue. CloseAwareWriteQueue is just a wrapper on top of DefaultWriteRequestQueue where the dispose() method does nothing.
>  
>   public void dispose(IoSession session) {
>   }
>
>
> So I have a theory for the mem leak:
>
> 1: Client request something,
> 2: Server fetch the data and call session.write(), the data got queued in the wirteRequestQueue.
> 3: Before the data got actually flushed, the client drops the connection
> 4: MINA fires sessionClosed() as expect, but the dispose method didn't actually clear the queue.
> 5: Because of the session still got referenced by the Finalizer, the huge amount of data just hang around.
>
>
> Am I right that the dispose() method should clear the queue?
>
> Thanks,
> Yong
>
>
>
>
> -----Original Message-----
> From: Yongxing Wang 
> Sent: Thursday, May 14, 2009 9:31 AM
> To: 'users@mina.apache.org'
> Subject: RE: memory leak? -- help needed
>
> Yes, I will definitely run it against one of those tools (my Jprofile evaluation expired, need to work it out). 
>
> I guess last night my brain was fried and I didn't provide enough information. The dump analysis told me that there are still more than 7K NioSocketSession (referenced by java.lang.Finalizer) hanging around three hours after I the process out of LB. This probably related to one of my earlier post:
>
> "Looking at the source code, it is caused by DefaultIoFilterChain (because it has a finalize method)." -- the chain holds a reference to the session.
>
> I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of dump data, the query never returns.
>
> I will continue to do more analysis. At the mean time, is there a reason that you have to use "finalize" method in the DefaultIoFilterChain? 
>
> Yong
>
>
> -----Original Message-----
> From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
> Sent: Wednesday, May 13, 2009 11:47 PM
> To: users@mina.apache.org
> Subject: Re: memory leak? -- help needed
>
> Yongxing Wang wrote:
>   
>> Gurus,
>>   
>>     
> hi,
>
> you should use a tool to determine if the memory leak you are 
> experiencing is part of your own code. You have plenty of them out there 
> : JProfiler, OptimizeIt, YourKit, ...
>
> This will give you some insight about the part of the code where this 
> memory leak is likely to be created.
>
> There is not much we can do with the provided information atm...
>
>   


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: memory leak? -- help needed

Posted by Emmanuel Lecharny <el...@apache.org>.
Hi,

this is very strange... If the client close the connection before the 
message is sent to him, then the session should be discarded.

Could you provide the code which initialize the chain you are using ?

Also could you run your test with the latest version (ie, the trunk)? We 
have fixed some bad issues lately about the way messages are handled.

Thanks !


Yongxing Wang wrote:

> In sessionClosed() method in DefaultIoFilterChain, it will do:
>
> s.getWriteRequestQueue().dispose(session);
>
>
> There are only two classes that implements WriteRequestQueue, one is CloseAwareWriteQueue and another is DefaultWriteRequestQueue. CloseAwareWriteQueue is just a wrapper on top of DefaultWriteRequestQueue where the dispose() method does nothing.
>  
>   public void dispose(IoSession session) {
>   }
>
>
> So I have a theory for the mem leak:
>
> 1: Client request something,
> 2: Server fetch the data and call session.write(), the data got queued in the wirteRequestQueue.
> 3: Before the data got actually flushed, the client drops the connection
> 4: MINA fires sessionClosed() as expect, but the dispose method didn't actually clear the queue.
> 5: Because of the session still got referenced by the Finalizer, the huge amount of data just hang around.
>
>
> Am I right that the dispose() method should clear the queue?
>
> Thanks,
> Yong
>
>
>
>
> -----Original Message-----
> From: Yongxing Wang 
> Sent: Thursday, May 14, 2009 9:31 AM
> To: 'users@mina.apache.org'
> Subject: RE: memory leak? -- help needed
>
> Yes, I will definitely run it against one of those tools (my Jprofile evaluation expired, need to work it out). 
>
> I guess last night my brain was fried and I didn't provide enough information. The dump analysis told me that there are still more than 7K NioSocketSession (referenced by java.lang.Finalizer) hanging around three hours after I the process out of LB. This probably related to one of my earlier post:
>
> "Looking at the source code, it is caused by DefaultIoFilterChain (because it has a finalize method)." -- the chain holds a reference to the session.
>
> I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of dump data, the query never returns.
>
> I will continue to do more analysis. At the mean time, is there a reason that you have to use "finalize" method in the DefaultIoFilterChain? 
>
> Yong
>
>
> -----Original Message-----
> From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
> Sent: Wednesday, May 13, 2009 11:47 PM
> To: users@mina.apache.org
> Subject: Re: memory leak? -- help needed
>
> Yongxing Wang wrote:
>   
>> Gurus,
>>   
>>     
> hi,
>
> you should use a tool to determine if the memory leak you are 
> experiencing is part of your own code. You have plenty of them out there 
> : JProfiler, OptimizeIt, YourKit, ...
>
> This will give you some insight about the part of the code where this 
> memory leak is likely to be created.
>
> There is not much we can do with the provided information atm...
>
>   


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



RE: memory leak? -- help needed

Posted by Yongxing Wang <yw...@sharpcast.com>.
In sessionClosed() method in DefaultIoFilterChain, it will do:

s.getWriteRequestQueue().dispose(session);


There are only two classes that implements WriteRequestQueue, one is CloseAwareWriteQueue and another is DefaultWriteRequestQueue. CloseAwareWriteQueue is just a wrapper on top of DefaultWriteRequestQueue where the dispose() method does nothing.
 
  public void dispose(IoSession session) {
  }


So I have a theory for the mem leak:

1: Client request something,
2: Server fetch the data and call session.write(), the data got queued in the wirteRequestQueue.
3: Before the data got actually flushed, the client drops the connection
4: MINA fires sessionClosed() as expect, but the dispose method didn't actually clear the queue.
5: Because of the session still got referenced by the Finalizer, the huge amount of data just hang around.


Am I right that the dispose() method should clear the queue?

Thanks,
Yong




-----Original Message-----
From: Yongxing Wang 
Sent: Thursday, May 14, 2009 9:31 AM
To: 'users@mina.apache.org'
Subject: RE: memory leak? -- help needed

Yes, I will definitely run it against one of those tools (my Jprofile evaluation expired, need to work it out). 

I guess last night my brain was fried and I didn't provide enough information. The dump analysis told me that there are still more than 7K NioSocketSession (referenced by java.lang.Finalizer) hanging around three hours after I the process out of LB. This probably related to one of my earlier post:

"Looking at the source code, it is caused by DefaultIoFilterChain (because it has a finalize method)." -- the chain holds a reference to the session.

I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of dump data, the query never returns.

I will continue to do more analysis. At the mean time, is there a reason that you have to use "finalize" method in the DefaultIoFilterChain? 

Yong


-----Original Message-----
From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
Sent: Wednesday, May 13, 2009 11:47 PM
To: users@mina.apache.org
Subject: Re: memory leak? -- help needed

Yongxing Wang wrote:
> Gurus,
>   
hi,

you should use a tool to determine if the memory leak you are 
experiencing is part of your own code. You have plenty of them out there 
: JProfiler, OptimizeIt, YourKit, ...

This will give you some insight about the part of the code where this 
memory leak is likely to be created.

There is not much we can do with the provided information atm...

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



RE: memory leak? -- help needed

Posted by Yongxing Wang <yw...@sharpcast.com>.
Yes, I will definitely run it against one of those tools (my Jprofile evaluation expired, need to work it out). 

I guess last night my brain was fried and I didn't provide enough information. The dump analysis told me that there are still more than 7K NioSocketSession (referenced by java.lang.Finalizer) hanging around three hours after I the process out of LB. This probably related to one of my earlier post:

"Looking at the source code, it is caused by DefaultIoFilterChain (because it has a finalize method)." -- the chain holds a reference to the session.

I tried to trace the "DefaultWriteFuture" to the GC root. With 10G of dump data, the query never returns.

I will continue to do more analysis. At the mean time, is there a reason that you have to use "finalize" method in the DefaultIoFilterChain? 

Yong


-----Original Message-----
From: Emmanuel Lecharny [mailto:elecharny@gmail.com] On Behalf Of Emmanuel Lecharny
Sent: Wednesday, May 13, 2009 11:47 PM
To: users@mina.apache.org
Subject: Re: memory leak? -- help needed

Yongxing Wang wrote:
> Gurus,
>   
hi,

you should use a tool to determine if the memory leak you are 
experiencing is part of your own code. You have plenty of them out there 
: JProfiler, OptimizeIt, YourKit, ...

This will give you some insight about the part of the code where this 
memory leak is likely to be created.

There is not much we can do with the provided information atm...

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: memory leak? -- help needed

Posted by Emmanuel Lecharny <el...@apache.org>.
Yongxing Wang wrote:
> Gurus,
>   
hi,

you should use a tool to determine if the memory leak you are 
experiencing is part of your own code. You have plenty of them out there 
: JProfiler, OptimizeIt, YourKit, ...

This will give you some insight about the part of the code where this 
memory leak is likely to be created.

There is not much we can do with the provided information atm...

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org