Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much memory is my program using Java

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

like image 643
Suman Palikhe Avatar asked Aug 11 '15 10:08

Suman Palikhe


People also ask

Does Java use a lot of memory?

Java is consistently listed as using 700MB of real memory.

What is memory usage in Java?

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.


1 Answers

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.

like image 94
Andrii Polunin Avatar answered Oct 04 '22 19:10

Andrii Polunin