I'm developing a clojure application and it seemed to be using a lot more memory than it should. I thought it was a memory leak but after looking at it with jvisualvm it seems like the GC just isn't running often enough. The red outlines are where I manually invoked the GC. Why is it allocating 300mb when it seems to be using around 30mb?
Make your heap smaller, if you think it's too big. GC only runs when it has to.
(The controls have changed over the years, but I think they're still in terms of an initial heap size, an increment size, and a max size. If your initial heap size is small, it will stay small so long as there's no "high water mark" in heap usage that pushes it higher.)
The JVM has parameters that regulate memory management, including amount initially allocated and max amount to allocate. Other than these parameters, the JVM has no way to know what you consider a reasonable amount of memory to use. To the best of my knowledge, there is no requirement for a JVM to run GC on any particular schedule, nor is any targetted amount of memory usage part of the definition of Java.
Suppose you've said that the max memory available to the JVM is, say, 100 MB, and you are presently using 50 MB including dead objects. You have a NEW that will require 1 MB. Should the JVM run a GC? It's not essential: you have plenty of room before you hit the maxium. Is it a good idea? That's very hard to say. Which is worse, to use a lot of memory or to do a lot of GCs?
As a Java programmer, you shouldn't normally know or care. As long as it does a GC before declaring that you are out of memory, what difference does it make? If your app is using so much memory that other apps can't allocate what they need, than alter the runtime parameters to reduce the maximum that you will take. That should be the limit of what matters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With