簡體   English   中英

查找分配給進程的虛擬內存量

[英]Finding how much virtual memory is allocated to a process

有沒有辦法找出為特定進程分配了多少虛擬內存? 我有一個程序,我還安裝了一個性能跟蹤器,它將監視進程正在使用的內存量以及剩余的可用空間。

為此,我需要知道為該進程分配了多少內存。 有沒有辦法用Java做到這一點? 我在Windows 7上運行。

另外,我目前一直在使用Sigar類來監視其他內存統計信息。 sigar是否具有可以找到我想要的特定類別/功能?

您可以使用Visualvm

在您的代碼中計算使用的內存:

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

要添加@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