I have been taking few different heap snapshots. What I see is that the memory usage is not the same as reported by this codes. So how to know the exact memory is being used by my java application. I also see my leak report from MAT reports a lot of finalize stuff is that any thing danger there.

Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
//runtime.gc();
// Calculate the used memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("\n\nUsed memory is bytes: " + memory);
//Print the jvm heap size.
long heapSize = runtime.totalMemory();
System.out.println("\n\nHeap Size = " + heapSize);

int mb = 1024*1024;
System.out.println("##### Heap utilization statistics [MB] #####");

//Print used memory
System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);

//Print free memory
System.out.println("Free Memory:" + runtime.freeMemory() / mb);

//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);

//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);

JVM memory usage is a lot more complex than this kind of analysis suggests. If you have a memory problem, you can use visualvm to perform all kinds of profiling in a way that may be more helpful to you. http://visualvm.java.net/

ps a version is included as standard with current JDKs - look for jvisualvm.exe in your JDK bin folder

Edited 3 Years Ago by JamesCherrill

Dear James,
          I have given up with setting visualvm.IT just failed to read and communicate with my remote application. I know its quite complex but in simple terms what normally to look for in terms of memory usage. Does the gcutil will do a help ?
Dear James,
          No there have been no oom more of curiousity and proactive measures to learn health status of the application.

OK, in that case maybe all you need is to to run gc() and check the free memory - but don't worry about individual results. Maybe run it regularly over a period of time see if any trend emerges from the noise.
Other than that, visualvm is the way to go for more detailed info - it is a real fully-supported tool, and it will work if you get it right ;)
(problems with a remote app may well be firewall-related)

Dear James,
          How you want me to run the gc via any command or build another application and run in it but that will defeat the purpose right? So far I have been monitoring the jstat tool no full gc have taken place but the perm generation some small increase. I have put a lot of effort in the visualvm and there is no firewall enabled in my remote server just the iptables but I have disabled it. 

I have given up with setting visualvm.IT just failed to read and communicate with my remote application.

It doesn't happen automagically. You need to configure the server JVM or the target application such that it can accept incoming VisualVM JMX requests. Take a look at this thread .

Edited 3 Years Ago by ~s.o.s~

Dear Sos,
        Thank you for the link done that many times the same thing repeat even with different ports. After many tries maybe I will start again soon. Anyway what is your opinion with regards to reading the jstat values of the perm and old generation? Anything I should be on the look ?
This article has been dead for over six months. Start a new discussion instead.