Java unit testing: how to measure memory footprint for method call
To measure current memory usage use :
Runtime.getRuntime().freeMemory()
,
Runtime.getRuntime().totalMemory()
Here is a good example: get OS-level system information
But this measurement is not precise but it can give you much information.
Another problem is with GC
which is unpredictable.
I can think of several options:
- Finding out how much memory your method requires via a microbenchmark (i.e. jmh).
- Building allocation strategies based on heuristic estimation. There are several open source solutions implementing class size estimation i.e. ClassSize. A much easier way could be utilizing a cache which frees rarely used objects (i.e. Guava's Cache). As mentioned by @EnnoShioji, Guava's cache has memory-based eviction policies.
You can also write your own benchmark test which counts memory. The idea is to
- Have a single thread running.
- Create a new array to store your objects to allocate. So these objects won't be collected during GC run.
System.gc()
,memoryBefore = runtime.totalMemory() - runtime.freeMemory()
- Allocate your objects. Put them into the array.
System.gc()
,memoryAfter = runtime.totalMemory() - runtime.freeMemory()
This is a technique I used in my lightweight micro-benchmark tool which is capable of measuring memory allocation with byte-precision.
You can use profiler (for ex. JProfiler) for view memory usage by classes. Or , how mentioned Areo, just print memory usage:
Runtime runtime = Runtime.getRuntime();
long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used Memory before" + usedMemoryBefore);
// working code here
long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Memory increased:" + (usedMemoryAfter-usedMemoryBefore));