0

有没有办法找出分配给特定进程的总虚拟内存量?我有一个程序,我还安装了一个性能跟踪器,它将监控进程正在使用多少内存以及剩余多少内存可供使用。

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

此外,我目前一直在使用 Sigar 类来监控其他内存统计信息。sigar 是否有一个特定的类/函数可以找到我正在寻找的东西?

4

2 回答 2

1

您可以使用Visualvm

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

Runtime runtime = Runtime.getRuntime(); // Get the Java runtime
long memory = runtime.totalMemory() - runtime.freeMemory(); // Calculate the used memory
于 2014-10-27T17:50:44.023 回答
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);
于 2014-10-27T18:06:39.390 回答