clamp
clamp

Reputation: 34006

Java VM can the heap get smaller?

if i run a java program the VM initially allocates some memory for the heap, right?

everytime the used memory would take more than available, the heap is increased, right?

is it possible that the heap can get smaller, once less memory is used?

thanks!

Upvotes: 3

Views: 763

Answers (2)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

Yes, garbage collector is free to reduce the size of heap if it discoveres after few collections that the application demand for memory way smaller than configured heap. You can observe this feature in VisualVM, here a Tomcat instance stress tested with ab:

VisualVM memory

As you can see the heap size (orange series) changes basically after every GC (drop in blue series). Sometimes heap size increases, sometimes decreases. JVM is free to adjust the size based on actual usage (here the total heap size was set to 1G).

Upvotes: 5

Shivan Dragon
Shivan Dragon

Reputation: 15219

Well the Java process start with an initial memory allocation for heap and a max alocation for said heap. It's what you pas via the -Xms and Xmx jvm command line arguments. These cannot change during process run.

The actual quantity of memory that is used by your object instances can decrease after a garbage collection.

Upvotes: 0

Related Questions