Memory Leak in java with LinkedBlockingQuene

S

sss.zhou

Memory Leak in with LinkedBlockingQuene

I have three questions which are memory leak and java GC concerned.

=======================================================

I knew there must be some memory leaks in my program. I discovered
that the instances of LinkedBlockingQuene@Node,
LinkedBlockingQuene@Segment and others was increased continuously (By
JProfiler) and will not decrease even after all requests were
processed. First I thought if there were still some elements I forgot
to remove, then I checked all the LinkedBlockingQuene count and they
are all zero.

I wanted to ask why @Node alive when the Quene is zero?

BTW: I was a little puzzled by the LinkedBlockingQuene::clear()
function.
It just set head and last to null and if all the @Nodes in the
middle are alive after called it.

I think Sun would not do such sunny thing, so why it work well?
/**
* Atomically removes all of the elements from this queue.
* The queue will be empty after this call returns.
*/
public void clear() {
fullyLock();
try {
head.next = null;
assert head.item == null;
last = head;
if (count.getAndSet(0) == capacity)
notFull.signalAll();
} finally {
fullyUnlock();
}
}

========================================================

The following content is Java GC concerned.

After I had done some test for some time, the count of the @Node
instance was 1000 (for example), then I stopped sending any request to
my server, then the count of the @Node kept 1000. But when I did a
full gc (by Profiler), the count changed to 800 (for example) and kept
800 after many times full gc.

My question is if the 200 instance can by free why JVM gc don't
free it in a idle time. If there are some error in my codes that VM
gc can't free it in a regular way and only can free it in Full GC.

========================================================

After I repaired the memory linked in my app. If Full GC (by VM or
not by call system.gc()) can by avoided?

I assigned 2G memory for my java App. After 10~12 hours its memory
will reach about 2G and VM did Full gc automatically. The used memory
decrease to 1.4G but it cost 20s and during this time my app was
unresponsive which was not allowed.

here is gc log:
[GC 1963039KK->1663039K(2097152K), 0.0445726 secs]
[Full GC 1963039K->1484445K(2097152K), 20.9461786 secs]
 
S

sss.zhou

Sorry for spelling mistake
LinkedBlockingQuene actually is : LinkedBlockingQueue
 
T

Tom Hawtin

I knew there must be some memory leaks in my program. I discovered
that the instances of LinkedBlockingQuene@Node,
LinkedBlockingQuene@Segment and others was increased continuously (By
JProfiler) and will not decrease even after all requests were
processed. First I thought if there were still some elements I forgot
to remove, then I checked all the LinkedBlockingQuene count and they
are all zero.

There doesn't appear to be a LinkedBlockingQueue.Segment.
I wanted to ask why @Node alive when the Quene is zero?

Where are the Nodes referenced from? Perhaps they haven't be collected
yet. 1.6 seems to be better in this regard than 1.5.
BTW: I was a little puzzled by the LinkedBlockingQuene::clear()
function.
It just set head and last to null and if all the @Nodes in the
middle are alive after called it.

head*.next* to null. There should be no other references to the nodes as
far as I can see.
My question is if the 200 instance can by free why JVM gc don't
free it in a idle time. If there are some error in my codes that VM
gc can't free it in a regular way and only can free it in Full GC.

You wouldn't want Java processors to take 100% CPU all the time, would
you? Doesn't make sense to run GC unnecessarily.
I assigned 2G memory for my java App. After 10~12 hours its memory
will reach about 2G and VM did Full gc automatically. The used memory
decrease to 1.4G but it cost 20s and during this time my app was
unresponsive which was not allowed.

There are many options for tuning GC in Sun's implementations.

Also Sun has a real-time implementation (Java RTS[1]). Azul has some
pause-free kit. And I believe BEA has a pause-free JRE.

Tom Hawtin

[1] http://java.sun.com/javase/technologies/realtime/
 
R

Roedy Green

BTW: I was a little puzzled by the LinkedBlockingQuene::clear()
function.
It just set head and last to null and if all the @Nodes in the
middle are alive after called it.

If that happened it means something ELSE besides the queue is pointing
to those elements, unless there is a Sun bug.

If you have some sort of chain between the objects, if only one is
kept alive, all will be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top