I was given a weird task. I have to refactor certain methods in a huge code base (the easy part) and provide performance gain reports. I should focus on speed of execution and memory usage. They want know the performance improvement by method!
So I have a method like this:
public void processHugeFile(File f) {
long start = java.lang.System.currentTimeMillis();
// lots of hashmaps, lots of arrays, weird logic,...
long end = java.lang.System.currentTimeMillis();
logger.log("performance comparison - exec time: " + (end - start));
}
Then I have to refactor it:
public void processHugeFile(File f) {
long start = java.lang.System.currentTimeMillis();
// just lists, some primitives, simple logic,...
long end = java.lang.System.currentTimeMillis();
logger.log("performance comparison - exec time: " + (end - start));
}
In the end I just have to process the logs.
But what about memory usage? I have tried getRuntime().totalMemory() - getRuntime().freeMemory()
and the getHeapMemoryUsage().getUsed()
but they don't seem to work. Also, JVM Profilers focus on objects not on methods and I am speaking of a fairly large code base.
Can someone provide me some hints?
Thank you very much.