Does a Java heap dump include thread stacks
Asked Answered
M

4

15

I have been using Eclipse Memory Analysis tool to examine a heap dump. I have not seen any cases where an object was kept alive by a local variable in a thread stack.

Are java thread stacks preserved in heap dumps? If not, do these objects get counted as unreachable objects in the dump? If so, is there any way to preserve the thread stacks so that uncollected garbage can be distinguished from local variable values?

Mouth answered 8/9, 2010 at 16:27 Comment(0)
M
16

Yes

Heap dumps from more recent JVM's (as of 2010) included Thread Stacks. Eclipse Memory Analyzer 0.8 (released in January 2010) included support for extracting this information: http://www.eclipse.org/mat/0.8/noteworthy.html

Mouth answered 10/8, 2012 at 14:19 Comment(1)
Even after seeing this answer I still needed some time to find the button. How is the "gears" button related to "thread stacks"?Karikaria
M
8

Stack traces are not preserved but object references in the stack are preserved.

SELECT DISTINCT * FROM OBJECTS ( SELECT OBJECTS
${snapshot}.getOutboundReferentIds(thread.getObjectId())
FROM INSTANCEOF java.lang.Thread thread )

This OQL query selects all objects referred to by Java threads (java.lang.Thread and subclasses). This set includes all Java Local variables along with any other objects referred to by Java thread instances.

Mouth answered 9/9, 2010 at 9:2 Comment(3)
Your query have syntax error in visualvm and jhat. Can you fix it?Singlehearted
Seems that this query for Eclipse MAT.Singlehearted
It is query for outgoing references from thread, it is not actually an execution stack. Same OQL for visualvm is map(heap.objects("java.lang.Thread"), '{th: it, loc: it.threadLocals}')Singlehearted
I
1

Nope, thread stacks are separate from heap dumps.

How are you making the heap dumps? jmap? If so, by default, only live objects are dumped. This means that you won't see unreachable objects. Sounds like you're having a memory leak or something. I would recommend using JVisualVM or a more sophisticated profiler.

Ibbie answered 8/9, 2010 at 16:58 Comment(0)
I
0

an object is certainly reachable even if only one local variable references it. try this:

MyClass
    main
        obj = new ...
        obj.doSomethingThatTakes30Minutes()

obj shoudl appear in the heap dump.

Iridissa answered 8/9, 2010 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.