Question

I know Stack memory stores primitive types and the addresses of objects and the object values are stored in heap memory.

But if primitive is part of object then where it will be stored in Heap or Stack ?

How can I verify it ?

Thanks.

Was it helpful?

Solution

The stack is used for only local primitives and references to objects that is in the scope of a method (or block within) and not a class. The heap is used for all object data including the primitive fields thereof.

OTHER TIPS

I think that it's not possible to know whether any data in the JVM is allocated on the stack or the heap. For example, for Hotspot, see: http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html#escapeAnalysis

In Hotspot, you could turn off Escape Analysis, in which case I think it will always be allocated on the heap, but I wouldn't like to guarantee it (it depends on the internal workings of the JVM).

As for determining if something is allocated on the heap, you could use jmap to take a heap dump, and analyze it using jhat (or another similar tool). Then you should be able to inspect the contents of the heap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top