简体   繁体   English

查找分配给进程的虚拟内存量

[英]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? 有没有办法用Java做到这一点? I am running on Windows 7. 我在Windows 7上运行。

Also, I have currently been using the Sigar classes to monitor other memory statistics. 另外,我目前一直在使用Sigar类来监视其他内存统计信息。 Does sigar have a specific class/function that can find what I am looking for? sigar是否具有可以找到我想要的特定类别/功能?

You can use Visualvm . 您可以使用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, 要添加@Mitesh提到的内容,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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