Ryan
Ryan

Reputation: 155

Is heap being shared to multiple threads?

I want to understand this, but when i set max heap size, does each thread in a multi-threaded environment creates their own heap and has the same size?

Upvotes: 0

Views: 1339

Answers (2)

Stephen C
Stephen C

Reputation: 718826

... does each thread in a multi-threaded environment creates their own heap and has the same size?

No. There is a single heap that is shared by all threads in the Java process.

It is possible to run a JVM with TLABs (thread local allocation buffers) to reduce allocation-related contention in a multi-threaded application. However, these are just regions with the Eden space of the Java heap. Not separate heaps. And besides, objects will be moved elsewhere by the GC as required.

References:

Upvotes: 4

Slava
Slava

Reputation: 837

Threads of the same process share the same virtual memory and the heap.

Actually, memory manager can maintain local memory pools dedicated to each individual thread. It tries to balance the pools. This is done "under the hood" as optimization, and it is invisible for you, so threads use entire heap allocated to the process.

Upvotes: 2

Related Questions