BlackLabrador
BlackLabrador

Reputation: 3108

Understanding how the Garbage Collector works in Java 7 when using Maps and Vectors

I have been reading some stuffs on how the garbage collector works in Java, but I'm not sure to correctly understand what it is doing in reality. So I've been creating an ugly program...

The only thing it does is:

After adding a few data and then removing everything, I am ending up with this memory consumption.

enter image description here

How come the program still use that amount of memory given that I don't have anymore references on the maps when removing them from the vector ?

Thanks for your help

EDIT AFTER USING VISUAL VM

enter image description here

Upvotes: 0

Views: 266

Answers (1)

Dave Morrissey
Dave Morrissey

Reputation: 4411

As laune has said, the memory won't be reclaimed by the operating system. The JVM reserves a minimum amount of memory for the heap at startup (configured with the Xms setting) and grows it as necessary up to the maximum (the Xmx setting). It will never release memory back to the operating system.

To see how much of the heap space is actually being used, there are various tools available. VisualVM is one of the best free ones, it will show you the heap space usage, and will even give you a live histogram showing the amount of memory used by objects of each class.

Upvotes: 1

Related Questions