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 stac开发者_如何学运维ks 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?
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
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.
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.
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.
精彩评论