[英]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.