简体   繁体   中英

Finding how much virtual memory is allocated to a process

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?

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM