How to compute or determine the memory consumption of a program written in Java using NetBeans?

1.1k views Asked by At

How might I compute or determine the memory consumption of a program written in Java using NetBeans?

4

There are 4 answers

0
Costis Aivalis On

Since you use Netbeans, there is a Profile Main Project button right next to the debug button of you tool bar. The option Memory allows you to monitor the memory graphically while your Main project runs. The Window-->Profiler-->Telemetry overview gives you a graph of your memory consumption similar to this one:

enter image description here

and even:

enter image description here

Among all sorts of other details...

0
Ian Macalinao On

Netbeans has an awesome built-in profiler. Start out by going to Profile -> Advanced Commands -> Run Profiler Calibration to get it set up. Once you're done, you can profile by clicking the stopwatch at the top of your screen, to the right of "Debug Project".

0
Ernesto Campohermoso On

If you want to check memory consumption and other statistics you cant try.

$JAVA_HOME/bin/jconsole

It let you choose the process PID of your application and see the Memory and CPU usage at runtime.

NOTE: You don't need to have Netbeans installed so it can be used in production environments also.

0
Phil On

If you want to compute the memory from your Java program you can calculate it at any time with this code:

double currentMemory = ( (double)((double)(Runtime.getRuntime().totalMemory()/1024)/1024))- ((double)((double)(Runtime.getRuntime().freeMemory()/1024)/1024));

It will give you the memory usage in megabytes. You could use this code to check the memory usage at different time and keep the maximum memory usage or make some statistics.

Also it will work even if you don't use NetBeans.