Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Java Heap Memory contiguous?

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?

like image 324
Tiago Farias Avatar asked Oct 18 '11 19:10

Tiago Farias


People also ask

Is Java heap contiguous?

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.

Is heap memory contiguous?

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.

How does Java heap memory work?

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.

Is everything stored on heap Java?

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.


3 Answers

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.

like image 54
ObscureRobot Avatar answered Sep 30 '22 00:09

ObscureRobot


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.

like image 24
alf Avatar answered Sep 30 '22 02:09

alf


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.

like image 36
proflux Avatar answered Sep 30 '22 01:09

proflux