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

时间:2014-10-27 17:42:34

标签: java memory memory-management sigar

有没有办法找出为特定进程分配了多少总虚拟内存?我有一个程序,我也在放置一个性能跟踪器,它将监视进程使用多少内存以及剩余多少内存供它使用。

要做到这一点,我需要知道为进程分配了多少内存。有没有办法在Java中这样做?我在Windows 7上运行。

另外,我目前一直在使用Sigar类来监视其他内存统计信息。 sigar是否有特定的类/功能可以找到我要找的东西?

2 个答案:

答案 0 :(得分:1)

您可以使用Visualvm

在您的代码中计算使用的内存: -

Runtime runtime = Runtime.getRuntime(); // Get the Java runtime
long memory = runtime.totalMemory() - runtime.freeMemory(); // Calculate the used memory

答案 1 :(得分:0)

要在@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);
相关问题