if primitive data is part of object where it is stored in java memory? [duplicate]

StackOverflow https://stackoverflow.com/questions/18646455

  •  27-06-2022
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top