I am made a program which uses A* search algorithm to solve 8 game puzzle. I was interested in seeing how much memory is being used by my program from start to finish.
So far i have done
In the beginning of the program
static double totalMem = Runtime.getRuntime().totalMemory()/(1024*1024);
static double memoryMax = Runtime.getRuntime().maxMemory()/(1024*1024);
and at the end of the program
timeTaken-=System.currentTimeMillis();
System.out.println("\n\n-------Total Time Taken = "+Math.abs(timeTaken)+
" millisec ------\n ");
System.out.println("-------Maximum Memory = "+memoryMax+" MB------\n ");
System.out.println("-------Total Memory = "+totalMem
+" MB------\n ");
currMem = Runtime.getRuntime().freeMemory()/(1024*1024);
System.out.println("-------Free Memory = "+currMem+" MB------\n ");
double memUsed = (Runtime.getRuntime().totalMemory())/(1024*1024)-currMem;
System.out.println("-------Total Used = "+memUsed
+" MB------\n ");
This doesn't seems to be right. When i test with different sets of data. Any sugessitions
Java is consistently listed as using 700MB of real memory.
A MemoryUsage object represents a snapshot of memory usage. Instances of the MemoryUsage class are usually constructed by methods that are used to obtain memory usage information about individual memory pool of the Java virtual machine or the heap or non-heap memory of the Java virtual machine as a whole.
It would be better to use profiler or similar software for this purpose. You can start with jvisualvm which is included in JDK or JProfiler.
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