0

Is there a way to find out how much total virtual memory is allocated to a specific process? I have a program and I am also putting in a performance tracker which will monitor how much memory the process is using and how much is left for it to use.

To do this, I need to know how much memory is allocated to the process. Is there a way to do this in Java? I am running on Windows 7.

Also, I have currently been using the Sigar classes to monitor other memory statistics. Does sigar have a specific class/function that can find what I am looking for?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
G Boggs
  • 381
  • 1
  • 6
  • 19

2 Answers2

1

You can use Visualvm.

In your code to calculate memory used:-

Runtime runtime = Runtime.getRuntime(); // Get the Java runtime
long memory = runtime.totalMemory() - runtime.freeMemory(); // Calculate the used memory
Mitesh Pathak
  • 1,306
  • 10
  • 14
  • oops, i deleted the comment, i was asking 'do we add this at the end of the method execution'.. tks +1 – spiderman Oct 27 '14 at 18:05
  • I have Eclipse Java EE IDE for Web Developers, Indigo. I instaled visual VM, but cannot launch it as mentioned here: http://visualvm.java.net/eclipse-launcher.html . Do you have any idea? – spiderman Oct 27 '14 at 18:56
0

To add more on what @Mitesh mentioned,

        int var=1; // for bytes. you can change here to print in MB or KB as you wish
        log.info("************************* PRINTING MEMORY USAGE - BEGIN **************");

        Runtime runtime = Runtime.getRuntime();

        /* Total number of processors or cores available to the JVM */
        log.info("Available processors (cores): "
                + runtime.availableProcessors());

        /* This will return Long.MAX_VALUE if there is no preset limit */
        long maxMemory = runtime.maxMemory();
        /* Maximum amount of memory the JVM will attempt to use */
        log.info("Maximum memory : "
                + (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory / var));

        /* Total memory currently available to the JVM */
        long totalMemory = runtime.totalMemory() / var;
        log.info("Total memory available to JVM : "
                + totalMemory);

        /* Total amount of free memory available to the JVM */
        long freeMemory = runtime.freeMemory() / var;
        log.info("Free memory : " + freeMemory);

        // Calculate the used memory
        long usedMemory = totalMemory - freeMemory; 
        log.info("Used memory : " + usedMemory);
Philiiiiiipp
  • 705
  • 1
  • 9
  • 24
spiderman
  • 10,892
  • 12
  • 50
  • 84