jvm

import java.io.*;

/**
* linux 下cpu 内存 磁盘 jvm的使用监控
* @author avery_leo
*
*/
public class TT {
/**
* 获取cpu使用情况
* @return
* @throws Exception
*/
public double getCpuUsage() throws Exception {
double cpuUsed = 0;

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令


BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;

while ((str = in.readLine()) != null) {
int m = 0;

if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&

strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 9) {// 第9列为CPU的使用百分比(RedHat

cpuUsed += Double.parseDouble(tmp);

}

}

}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
}
/**
* 内存监控
* @return
* @throws Exception
*/
public double getMemUsage() throws Exception {

double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令


BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;

while ((str = in.readLine()) != null) {
int m = 0;

if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
//
// System.out.println("------------------3-----------------");
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;

if (++m == 10) {
// 9)--第10列为mem的使用百分比(RedHat 9)

menUsed += Double.parseDouble(tmp);

}
}

}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return menUsed;
}

/**
* 获取磁盘空间大小
*
* @return
* @throws Exception
*/
public double getDeskUsage() throws Exception {
double totalHD = 0;
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");//df -hl 查看硬盘空间


BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
int flag = 0;
while ((str = in.readLine()) != null) {
int m = 0;
// if (flag > 0) {
// flag++;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
++m;
// System.out.println("----tmp----" + tmp);
if (tmp.indexOf("G") != -1) {
if (m == 2) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() - 1)) * 1024;

}
if (m == 3) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1)) * 1024;

}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() - 1));

}
if (m == 3) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1));
// System.out.println("----3----" + usedHD);
}
}

}

// }
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return (usedHD / totalHD) * 100;
}

public static void main(String[] args) throws Exception {
TT cpu = new TT();
System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%");
System.out.println("---------------mem used:" + cpu.getMemUsage() + "%");
System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%");
System.out.println("------------jvm监控----------------------");
Runtime lRuntime = Runtime.getRuntime();
System.out.println("--------------Free Momery:" + lRuntime.freeMemory()+"K");
System.out.println("--------------Max Momery:" + lRuntime.maxMemory()+"K");
System.out.println("--------------Total Momery:" + lRuntime.totalMemory()+"K");
System.out.println("---------------Available Processors :"
+ lRuntime.availableProcessors());
}
}
06-11
### 什么是 JVM? Java 虚拟机(JVM)是 Java 运行时环境的核心组件,它负责运行 Java 程序[^2]。JVM 是一个抽象的计算机,它通过调用某个初始类的 `main()` 方法来运行 Java 程序[^3]。该方法必须满足以下条件:共有的(`public`)、静态的(`static`)、返回值为 `void`,并且接受一个字符串数组作为参数。 ### JVM 的组成 JVM 主要由以下几个部分组成: 1. **类加载器(Class Loader)** 类加载器负责将 `.class` 文件加载到内存中,并将其转换为可执行的字节码。类加载器分为引导类加载器、扩展类加载器和应用程序类加载器[^2]。 2. **运行时数据区(Runtime Data Areas)** JVM 在运行时会创建多个内存区域来存储数据,包括: - **方法区(Method Area)**:存储已被虚拟机加载的类信息、常量、静态变量等。 - **堆(Heap)**:所有对象实例以及数组都在堆上分配。 - **虚拟机栈(Java Virtual Machine Stacks)**:每个线程都有一个私有的虚拟机栈,存储局部变量表、操作数栈等。 - **本地方法栈(Native Method Stacks)**:与虚拟机栈类似,但服务于本地方法。 - **程序计数器(Program Counter Register)**:记录当前线程所执行的字节码指令地址。 3. **执行引擎(Execution Engine)** 执行引擎负责解释或编译字节码并执行。主要包括: - **解释器(Interpreter)**:逐条解释执行字节码。 - **即时编译器(JIT Compiler)**:将热点代码编译为本地机器码以提高性能[^4]。 4. **本地接口(Native Interface)** JVM 提供了与本地库交互的能力,例如通过 JNI(Java Native Interface)调用 C/C++ 编写的本地代码。 5. **垃圾回收机制(Garbage Collection, GC)** JVM 自动管理内存,通过垃圾回收器释放不再使用的对象占用的内存[^2]。 ### JVM 参数与优化 为了更好地控制 JVM 的行为,开发者可以使用各种 JVM 参数进行调优。这些参数涵盖了内存分配、垃圾回收策略、线程管理等多个方面[^1]。例如: - `-Xms` 和 `-Xmx`:设置堆内存的初始大小和最大大小。 - `-XX:+UseG1GC`:指定使用 G1 垃圾回收器。 - `-verbose:gc`:输出垃圾回收的日志信息。 ### 示例代码:启动 JVM 并运行 Java 程序 以下是一个简单的 Java 程序示例,展示了如何定义一个符合 JVM 规范的入口点: ```java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } ``` 编译并运行该程序时,JVM 会加载 `HelloWorld` 类,并调用其 `main()` 方法作为程序的起点。 ### 同步与锁机制 在多线程环境中,JVM 提供了同步机制以确保线程安全。`synchronized` 关键字可以通过 ACC_SYNCHRONIZED 标志实现方法级别的同步,而无需显式调用 `monitorenter` 和 `monitorexit` 指令[^5]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值