I'm exploring memory usage in Java to understand why my program leaks memory. After stripping off code in my main while loop, I still get an increase of memory usage over time. Pondering the memory usage of an empty program:
class Nothing
{ public static void main(String[] args)
{ while(true); }
}
I still saw an increase of memory:
So my question is: Why is there still a saw tooth pattern? Why when the GC runs does it not save all the memory (each time the gc runs (the valleys) the used memory increases by 10-20Kb (compared to the previous valley))?
EDIT:
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Client VM (build 20.4-b02, mixed mode, sharing)
OS: Windows 7 Enterprise-32 bit
Java is also a very high-level Object-Oriented programming language (OOP) which means that while the application code itself is much easier to maintain, the objects that are instantiated will use that much more memory.
In general, a Java memory leak happens when an application unintentionally (due to logical errors in code) holds on to object references that are no longer required. These unintentional object references prevent the built-in Java garbage collection mechanism from freeing up the memory consumed by these objects.
This is an artifact by the profiler, without the profiler there will be no allocations. Different profilers produce different artifacts, depending on how they record data. Below you see what profiling Nothing
JProfiler will look like:
Much less of a sawtooth pattern. However, there is also a minuscule memory consumption:
which is due to the fact the profiling agent polls the java.lang.management.MemoryUsage
JMX bean. Eventually this consumption will also trigger a garbage collection.
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