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!
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: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).