I've seen people comment about Java Heap memory being contiguous and people say the contrary. Well, can someone give me a final answer and explain this question? Also, is there some kind of data structure in Java Heap as there is in Java Stack?
Heap memory is a part of memory allocated to JVM, which is shared by all executing threads in the application. It is the part of JVM in which all class instances and are allocated. It is created on the Start-up process of JVM. It does not need to be contiguous, and its size can be static or dynamic.
A single stack or heap memory allocation is always contiguous ... in every programming language runtime I've ever encountered where it makes sense to talk about allocations at all. A sequence of allocations is contiguous if there is no gap between the individual allocations.
Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
The Heap Space contains all objects are created, but Stack contains any reference to those objects. Objects stored in the Heap can be accessed throughout the application.
Quoting from the JVM spec: The memory for the heap does not need to be contiguous. So your code shouldn't make assumptions about the continuity of the heap.
The real answer is, you don't know and should not care. There are different JVMs, and no one makes promise on anything but specification.
It depends on the JVM but is definitely not guaranteed to be contiguous. HotSpot uses a generational heap whereas IBM JDK and JRockit do not. I believe both the IBM and JRockit garbage collection processes uses a sweep/compress algorithm which in practice should result in a contiguous heap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With