0 votes
1 view
in AI and Deep Learning by (43.2k points)

I am made a program that 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

At 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


        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 seem to be right. When i test with different sets of data. Any suggestions?

1 Answer

0 votes
by (92.8k points)

It would be better to use profiler or similar software for this purpose. You can begin with JVisualVM which is included in JDK or JProfiler. You can also use "Java Mission Control" (newer version of JVisualVM), which allows you to take snapshots of your memory. Add these parameters at startup of your program:



-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9010 




It is in the "bin" folder of your JDK 7 too, called: "jmc.exe"

Welcome to Intellipaat Community. Get your technical queries answered by top developers !