以下的程序监控参数的代码,有些是从网络上获取的,此处进行一个记录是为了以后如果要用到方便记录。

1、引入jar包,为了获取一些cpu的使用率等信息

  <dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.12.2</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.2.0</version>
</dependency> <dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.2.0</version>
</dependency>

2、编写代码

/**
* 系统监控
*
* @author huan.fu
* @date 2018/12/3 - 18:44
*/
@Component
public class SystemMonitor { @PostConstruct
public void init() {
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
try { SystemInfo systemInfo = new SystemInfo(); OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
//椎内存使用情况
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); //初始的总内存
long initTotalMemorySize = memoryUsage.getInit();
//最大可用内存
long maxMemorySize = memoryUsage.getMax();
//已使用的内存
long usedMemorySize = memoryUsage.getUsed(); // 操作系统
String osName = System.getProperty("os.name");
// 总的物理内存
String totalMemorySize = new DecimalFormat("#.##").format(osmxb.getTotalPhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
// 剩余的物理内存
String freePhysicalMemorySize = new DecimalFormat("#.##").format(osmxb.getFreePhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
// 已使用的物理内存
String usedMemory = new DecimalFormat("#.##").format((osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / 1024.0 / 1024 / 1024) + "G";
// 获得线程总数
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent()) { } int totalThread = parentThread.activeCount(); // 磁盘使用情况
File[] files = File.listRoots();
for (File file : files) {
String total = new DecimalFormat("#.#").format(file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
String free = new DecimalFormat("#.#").format(file.getFreeSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
String un = new DecimalFormat("#.#").format(file.getUsableSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
String path = file.getPath();
System.err.println(path + "总:" + total + ",可用空间:" + un + ",空闲空间:" + free);
System.err.println("=============================================");
} System.err.println("操作系统:" + osName);
System.err.println("程序启动时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(ManagementFactory.getRuntimeMXBean().getStartTime())));
System.err.println("pid:" + System.getProperty("PID"));
System.err.println("cpu核数:" + Runtime.getRuntime().availableProcessors());
printlnCpuInfo(systemInfo);
System.err.println("JAVA_HOME:" + System.getProperty("java.home"));
System.err.println("JAVA_VERSION:" + System.getProperty("java.version"));
System.err.println("USER_HOME:" + System.getProperty("user.home"));
System.err.println("USER_NAME:" + System.getProperty("user.name"));
System.err.println("初始的总内存(JVM):" + new DecimalFormat("#.#").format(initTotalMemorySize * 1.0 / 1024 / 1024) + "M");
System.err.println("最大可用内存(JVM):" + new DecimalFormat("#.#").format(maxMemorySize * 1.0 / 1024 / 1024) + "M");
System.err.println("已使用的内存(JVM):" + new DecimalFormat("#.#").format(usedMemorySize * 1.0 / 1024 / 1024) + "M");
System.err.println("总的物理内存:" + totalMemorySize);
System.err.println("总的物理内存:" + new DecimalFormat("#.##").format(systemInfo.getHardware().getMemory().getTotal() * 1.0 / 1024 / 1024 / 1024) + "M");
System.err.println("剩余的物理内存:" + freePhysicalMemorySize);
System.err.println("剩余的物理内存:" + new DecimalFormat("#.##").format(systemInfo.getHardware().getMemory().getAvailable() * 1.0 / 1024 / 1024 / 1024) + "M");
System.err.println("已使用的物理内存:" + usedMemory);
System.err.println("已使用的物理内存:" + new DecimalFormat("#.##").format((systemInfo.getHardware().getMemory().getTotal() - systemInfo.getHardware().getMemory().getAvailable()) * 1.0 / 1024 / 1024 / 1024) + "M");
System.err.println("总线程数:" + totalThread);
System.err.println("===========================");
} catch (Exception e) {
e.printStackTrace();
}
}, 0, 60, TimeUnit.SECONDS);
} /**
* 打印 CPU 信息
*
* @param systemInfo
*/
private void printlnCpuInfo(SystemInfo systemInfo) throws InterruptedException {
CentralProcessor processor = systemInfo.getHardware().getProcessor();
long[] prevTicks = processor.getSystemCpuLoadTicks();
// 睡眠1s
TimeUnit.SECONDS.sleep(1);
long[] ticks = processor.getSystemCpuLoadTicks();
long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
System.err.println("cpu核数:" + processor.getLogicalProcessorCount());
System.err.println("cpu系统使用率:" + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
System.err.println("cpu用户使用率:" + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
System.err.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
System.err.println("cpu当前空闲率:" + new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
System.err.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100);
System.err.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100);
}
}

3、获取更多系统信息,参考如下网址

https://github.com/oshi/oshi

java监控JVM的内存使用情况等的更多相关文章

  1. 如何设置Java虚拟机JVM启动内存参数

    Tomcat默认的Java虚拟机JVM启动内存参数大约只有64MB或者128MB,非常小,远远没有利用现在服务器的强大内存,所以要设置Java虚拟机JVM启动内存参数.具体设置方法为: Tomcat修 ...

  2. java中JVM虚拟机内存模型详细说明

    java中JVM虚拟机内存模型详细说明 2012-12-12 18:36:03|  分类: JAVA |  标签:java  jvm  堆内存  虚拟机  |举报|字号 订阅     JVM的内部结构 ...

  3. 【java】JVM的内存区域划分

    学过C语言的朋友都知道C编译器在划分内存区域的时候经常将管理的区域划分为数据段和代码段,数据段包括堆.栈以及静态数据区.那么在Java语言当中,内存又是如何划分的呢? 由于Java程序是交由JVM执行 ...

  4. 巩固java(二)----JVM堆内存结构及垃圾回收机制

    前言:        我们在运行程序时,有时会碰到内存溢出(OutOfMemoryError)的问题,为了解决这种问题,我们有必要了解JVM的内存结构和垃圾回收机制. 正文: 1.JVM堆内存结构   ...

  5. Java:JVM的内存模型

    JVM内存模型 JVM内存模型可以分为两个部分,如下图所示,堆和方法区是所有线程共有的,而虚拟机栈,本地方法栈和程序计数器则是线程私有的.   1. 堆(Heap) 堆内存是所有线程共有的,可以分为两 ...

  6. 使用Jvisualvm监控JVM的内存、CPU、线程

    最近做性能测试发现很多性能问题,面对一些开发小白的数据结构思想,真想喊一声:放开那个代码,让我来!冲动. 面对WEB站点开发,性能测试是经常要做的,下面一种介绍如何结合性能测试工具,更好的监控WEB服 ...

  7. Windows查看Java内存使用情况

    Windows查看Java程序运行时内存使用情况 1.在cmd命令窗口输入 jconsole  ,弹出Java监视和管理控制台窗口 2.连接本地进程,首先需要知道想查看的进程ID ( pid ) 在c ...

  8. java之 JVM 内存管理详解

    一.JVM结构 根据<java虚拟机规范>规定,JVM的基本结构一般如下图所示: 从左图可知,JVM主要包括四个部分: 1.类加载器(ClassLoader):在JVM启动时或者在类运行时 ...

  9. nagios 监控内存使用情况

    监控本机内存cd /usr/lib64/nagios/pluginstouch check_mem.sh #!/bin/bash " ]; then memTotal_b=`free -b ...

随机推荐

  1. 如何实现 iOS 短视频跨页面的无痕续播?

    在一切皆可视频化的今天,短视频内容作为移动端产品新的促活点,受到了越来越多的重视与投入.盒马在秒播.卡顿率.播放成功率等基础优化之外,在用户使用体验上引入了无痕续播能力,提升用户观看视频内容的延续性. ...

  2. 最全Windows版本jemalloc库(5.2.1)及其使用:包含动态库和静态库、x86版本和x64版本、debug版本和release版本

    编写服务器程序时,需要频繁的申请和释放内存,长时间运行会产生大量的内存碎片,这就导致即使当前系统中的闲置内存还足够多,但也无法申请到大的连续可用的内存块,因为此时的物理内存已经千疮百孔像个马蜂窝.此外 ...

  3. ysoserial payloads/JRMPClient

    ysoserial payloads/JRMPClient 环境:JDK8u102 payloads/JRMPClient可以配合exploit/JRMPListener模块来使用 1.在自己服务器上 ...

  4. RSTP

    一.STP协议的缺点,存在的问题 STP 协议工作时间收敛慢,响应时间长---------->RSTP 原始的802.1d(stp)不支持多个vlan---->(PVST===>把一 ...

  5. 关于PHP数组Key的强制类型转换

    PHP是弱类型语言,就像JavaScript一样,在定义变量时,不需要强制指定变量的类型.同时,PHP又有着强大的数组功能,数组的Key即可以是普通的数字类型下标,也可以是字符串类型的Hash键值,那 ...

  6. obs软件mac设置

    1 远程电脑软件teamview或者向日葵 远程mac电脑会时黑屏或连不上, 需要设置系统偏好->设置软件权限 2 设置obs 显示不出画面或声音,也需要在系统偏好设置obs的权限(首先打开OB ...

  7. LINUX服务器带宽跑满、负载过高问题排查

    1.centos 安装流量监控iftop apt-get  install iftop -y 2.查看网卡名称 ifconfig 3.查看端口占用情况 iftop -i 网卡名称 -P 执行 nets ...

  8. VS 2019下载、安装与注册包含MF、界面美化和安装Visual Assist

    下载: 1.在搜索框中输入"微软" 2. 3. 安装: 1.双击运行-继续-等待安装完成 2. 3.安装完后,重启电脑,并创建快捷方式. 注册: 1.打开软件 2. 3. 4.网上 ...

  9. Java基础系列(8)- 数据类型

    数据类型 强类型语言 要求变量的使用合乎规定,所有的变量都必须先定义才能使用.Java是强类型语言. 弱类型语言 变量定义比较随意,比如"12"+3,可以是int型123,也可以是 ...

  10. 浏览器+css基础+选择器+权重+匹配规则

    浏览器的组成: shell+内核 shell:用户能看得到的界面就叫shell 内核:渲染rendering引擎和js引擎 现在主流拥有自己开发内核的浏览器:opera现在属于360和昆仑万维 CSS ...