当前位置:  开发笔记 > 编程语言 > 正文

如何在Java中检查CPU和内存使用情况?

如何解决《如何在Java中检查CPU和内存使用情况?》经验,为你挑选了7个好方法。

我需要检查java中服务器的CPU和内存使用情况,有谁知道怎么做?



1> Jeremy..:

如果您在JVM内存中专门寻找:

Runtime runtime = Runtime.getRuntime();

NumberFormat format = NumberFormat.getInstance();

StringBuilder sb = new StringBuilder();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();

sb.append("free memory: " + format.format(freeMemory / 1024) + "
"); sb.append("allocated memory: " + format.format(allocatedMemory / 1024) + "
"); sb.append("max memory: " + format.format(maxMemory / 1024) + "
"); sb.append("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "
");

但是,这些应该只是一个估计......


请注意,这不是实际使用的内存 - 这是'已分配的内存',这意味着java已分配的堆,因此如果您有-Xms90g且您的应用程序非常轻量级,您仍然会将allocateMemory作为大于90g的内容.通过删除下面的"未知(雅虎)"看到答案(乍一看可能会有所不同)

2> 小智..:
package mkd.Utils;

import java.io.File;
import java.text.NumberFormat;

public class systemInfo {

    private Runtime runtime = Runtime.getRuntime();

    public String Info() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.OsInfo());
        sb.append(this.MemInfo());
        sb.append(this.DiskInfo());
        return sb.toString();
    }

    public String OSname() {
        return System.getProperty("os.name");
    }

    public String OSversion() {
        return System.getProperty("os.version");
    }

    public String OsArch() {
        return System.getProperty("os.arch");
    }

    public long totalMem() {
        return Runtime.getRuntime().totalMemory();
    }

    public long usedMem() {
        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    }

    public String MemInfo() {
        NumberFormat format = NumberFormat.getInstance();
        StringBuilder sb = new StringBuilder();
        long maxMemory = runtime.maxMemory();
        long allocatedMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        sb.append("Free memory: ");
        sb.append(format.format(freeMemory / 1024));
        sb.append("
"); sb.append("Allocated memory: "); sb.append(format.format(allocatedMemory / 1024)); sb.append("
"); sb.append("Max memory: "); sb.append(format.format(maxMemory / 1024)); sb.append("
"); sb.append("Total free memory: "); sb.append(format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024)); sb.append("
"); return sb.toString(); } public String OsInfo() { StringBuilder sb = new StringBuilder(); sb.append("OS: "); sb.append(this.OSname()); sb.append("
"); sb.append("Version: "); sb.append(this.OSversion()); sb.append("
"); sb.append(": "); sb.append(this.OsArch()); sb.append("
"); sb.append("Available processors (cores): "); sb.append(runtime.availableProcessors()); sb.append("
"); return sb.toString(); } public String DiskInfo() { /* Get a list of all filesystem roots on this system */ File[] roots = File.listRoots(); StringBuilder sb = new StringBuilder(); /* For each filesystem root, print some info */ for (File root : roots) { sb.append("File system root: "); sb.append(root.getAbsolutePath()); sb.append("
"); sb.append("Total space (bytes): "); sb.append(root.getTotalSpace()); sb.append("
"); sb.append("Free space (bytes): "); sb.append(root.getFreeSpace()); sb.append("
"); sb.append("Usable space (bytes): "); sb.append(root.getUsableSpace()); sb.append("
"); } return sb.toString(); } }



3> 小智..:

如果您正在使用Sun JVM,并且对应用程序的内部内存使用感兴趣(应用程序使用的内存分配量多少),我更喜欢打开JVM内置垃圾收集日志记录.您只需将-verbose:gc添加到启动命令即可.

来自Sun的文档:

命令行参数-verbose:gc在每个集合上打印信息.请注意,-verbose:gc输出的格式可能会在J2SE平台的发行版之间发生变化.例如,这是从大型服务器应用程序输出:

[GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs]

在这里,我们看到两个小集合和一个主要集合.箭头前后的数字

325407K->83000K (in the first line)

表示垃圾收集前后活动对象的总大小.在次要集合之后,计数包括不一定存活但不能被回收的对象,因为它们直接存活,或者因为它们在终身代内或从其中引用.括号中的数字

(776768K) (in the first line)

是总可用空间,不计算永久代的空间,即总堆减去一个幸存者空间.次要收藏大约花了四分之一秒.

0.2300771 secs (in the first line)

有关详细信息,请参阅:http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html



4> danieln..:

从这里开始

    OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
    long prevUpTime = runtimeMXBean.getUptime();
    long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();
    double cpuUsage;
    try
    {
        Thread.sleep(500);
    }
    catch (Exception ignored) { }

    operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    long upTime = runtimeMXBean.getUptime();
    long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
    long elapsedCpu = processCpuTime - prevProcessCpuTime;
    long elapsedTime = upTime - prevUpTime;

    cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));
    System.out.println("Java CPU: " + cpuUsage);



5> Javamann..:

JMX,提供的MXBeans(ThreadMXBean等)将为您提供内存和CPU使用.

OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
operatingSystemMXBean.getSystemCpuLoad();



6> Rich Adams..:

对于内存使用,以下将起作用,

long total = Runtime.getRuntime().totalMemory();
long used  = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

对于CPU使用情况,您需要使用外部应用程序来测量它.



7> Telcontar..:

从Java 1.5开始,JDK附带了一个新工具:JConsole可以向您展示任何1.5或更高版本JVM的CPU和内存使用情况.它可以做这些参数的图表,导出到CSV,显示加载的类数,实例数,死锁,线程等......

推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有