java获得CPU使用率,系统内存,虚拟…

简介: 程序计算时间: longstartTime=System.currentTimeMillis();  //获取开始时间 。。。。。。。。。。。。
程序计算时间:

longstartTime=System.currentTimeMillis();  //获取开始时间

。。。。。。。。。。。。
long endTime=System.currentTimeMillis(); //获取结束时间

System.out.println("程序运行时间: "+(endTime-startTime)+"ms(毫秒)");






 利用java程序实现获取计算机cpu利用率和内存使用信息。  
 
   创建一个Bean用来存贮要得到的信   
 
public class MonitorInfoBean {  
      
    private longtotalMemory;   
    
      
    private longfreeMemory;   
    
      
    private longmaxMemory;   
    
      
    privateString osName;   
    
      
    private longtotalMemorySize;   
    
      
    private longfreePhysicalMemorySize;   
    
      
    private longusedMemory;   
    
      
    private inttotalThread;   
    
      
    privatedouble cpuRatio;   
 
    public longgetFreeMemory() {   
       return freeMemory;   
   }   
 
    public voidsetFreeMemory(long freeMemory) {  
       this.freeMemory = freeMemory;  
   }   
 
    public longgetFreePhysicalMemorySize() {  
       return freePhysicalMemorySize;  
   }   
 
    public voidsetFreePhysicalMemorySize(long freePhysicalMemorySize){   
       this.freePhysicalMemorySize =freePhysicalMemorySize;   
   }   
 
    public longgetMaxMemory() {   
       return maxMemory;   
   }   
 
    public voidsetMaxMemory(long maxMemory) {  
       this.maxMemory = maxMemory;  
   }   
 
    publicString getOsName() {   
       return osName;   
   }   
 
    public voidsetOsName(String osName) {  
       this.osName = osName;   
   }   
 
    public longgetTotalMemory() {   
       return totalMemory;   
   }   
 
    public voidsetTotalMemory(long totalMemory) {  
       this.totalMemory = totalMemory;  
   }   
 
    public longgetTotalMemorySize() {   
       return totalMemorySize;   
   }   
 
    public voidsetTotalMemorySize(long totalMemorySize) {  
       this.totalMemorySize = totalMemorySize;  
   }   
 
    public intgetTotalThread() {   
       return totalThread;   
   }   
 
    public voidsetTotalThread(int totalThread) {  
       this.totalThread = totalThread;  
   }   
 
    public longgetUsedMemory() {   
       return usedMemory;   
   }   
 
    public voidsetUsedMemory(long usedMemory) {  
       this.usedMemory = usedMemory;  
   }   
 
    publicdouble getCpuRatio() {   
       return cpuRatio;   
   }   
 
    public voidsetCpuRatio(double cpuRatio) {  
       this.cpuRatio = cpuRatio;  
   }   
}   
 
   之后,建立bean的接口   
 
public interface IMonitorService {  
    publicMonitorInfoBean getMonitorInfoBean() throwsException;   
 
}   
 
 然后,就是最关键的,得到cpu的利用率,已用内存,可用内存,最大内存等信息。  
 
import java.io.InputStreamReader;  
import java.io.LineNumberReader;  
 
import sun.management.ManagementFactory;  
 
import com.sun.management.OperatingSystemMXBean;  
import java.io.*;   
import java.util.StringTokenizer;  
 
   
public class MonitorServiceImpl implements IMonitorService{   
    
    privatestatic final int CPUTIME = 30;  
 
    privatestatic final int PERCENT = 100;  
 
    privatestatic final int FAULTLENGTH = 10;  
    
    privatestatic final File versionFile = newFile("/proc/version");   
    privatestatic String linuxVersion = null;  
 
      
    publicMonitorInfoBean getMonitorInfoBean() throws Exception{   
       int kb = 1024;   
        
       // 可使用内存   
       long totalMemory = Runtime.getRuntime().totalMemory() /kb;   
       // 剩余内存   
       long freeMemory = Runtime.getRuntime().freeMemory() /kb;   
       // 最大可使用内存   
       long maxMemory = Runtime.getRuntime().maxMemory() /kb;   
 
       OperatingSystemMXBean osmxb = (OperatingSystemMXBean)ManagementFactory   
               .getOperatingSystemMXBean();  
 
       // 操作系统   
       String osName = System.getProperty("os.name");  
       // 总的物理内存   
       long totalMemorySize = osmxb.getTotalPhysicalMemorySize() /kb;   
       // 剩余的物理内存   
       long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() /kb;   
       // 已使用的物理内存   
       long usedMemory = (osmxb.getTotalPhysicalMemorySize() -osmxb   
               .getFreePhysicalMemorySize())  
               / kb;   
 
       // 获得线程总数   
       ThreadGroup parentThread;  
       for (parentThread = Thread.currentThread().getThreadGroup();parentThread   
               .getParent() != null; parentThread =parentThread.getParent())  
           ;   
       int totalThread = parentThread.activeCount();  
 
       double cpuRatio = 0;   
       if (osName.toLowerCase().startsWith("windows")) {  
           cpuRatio = this.getCpuRatioForWindows();  
       }   
       else {   
        cpuRatio = this.getCpuRateForLinux();  
       }   
        
       // 构造返回对象   
       MonitorInfoBean infoBean = new MonitorInfoBean();  
       infoBean.setFreeMemory(freeMemory);  
       infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);  
       infoBean.setMaxMemory(maxMemory);  
       infoBean.setOsName(osName);  
       infoBean.setTotalMemory(totalMemory);  
       infoBean.setTotalMemorySize(totalMemorySize);  
     
目录
相关文章
|
14天前
|
存储 Java 编译器
Java内存区域详解
Java内存区域详解
29 0
Java内存区域详解
|
24天前
|
缓存 算法 Java
Java内存管理与调优:释放应用潜能的关键
【4月更文挑战第2天】Java内存管理关乎性能与稳定性。理解JVM内存结构,如堆和栈,是优化基础。内存泄漏是常见问题,需谨慎管理对象生命周期,并使用工具如VisualVM检测。有效字符串处理、选择合适数据结构和算法能提升效率。垃圾回收自动回收内存,但策略调整影响性能,如选择不同类型的垃圾回收器。其他优化包括调整堆大小、使用对象池和缓存。掌握这些技巧,开发者能优化应用,提升系统性能。
|
1月前
|
监控 Java 数据库连接
解析与预防:Java中的内存泄漏问题
解析与预防:Java中的内存泄漏问题
|
1月前
|
监控 Java 索引
cpu使用率过高和jvm old占用过高排查过程
cpu使用率过高和jvm old占用过高排查过程
37 2
|
20天前
|
缓存 安全 Java
Java并发编程进阶:深入理解Java内存模型
【4月更文挑战第6天】Java内存模型(JMM)是多线程编程的关键,定义了线程间共享变量读写的规则,确保数据一致性和可见性。主要包括原子性、可见性和有序性三大特性。Happens-Before原则规定操作顺序,内存屏障和锁则保障这些原则的实施。理解JMM和相关机制对于编写线程安全、高性能的Java并发程序至关重要。
|
28天前
|
缓存 Java C#
【JVM故障问题排查心得】「Java技术体系方向」Java虚拟机内存优化之虚拟机参数调优原理介绍(一)
【JVM故障问题排查心得】「Java技术体系方向」Java虚拟机内存优化之虚拟机参数调优原理介绍
79 0
|
2天前
|
Java 程序员 数据库连接
Java从入门到精通:3.3.2性能优化与调优——内存管理篇
Java从入门到精通:3.3.2性能优化与调优——内存管理篇
Java从入门到精通:3.3.2性能优化与调优——内存管理篇
|
3天前
|
存储 安全 Java
滚雪球学Java(19):JavaSE中的内存管理:你所不知道的秘密
【4月更文挑战第8天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
30 4
滚雪球学Java(19):JavaSE中的内存管理:你所不知道的秘密
|
10天前
|
存储 缓存 监控
Java内存管理:垃圾回收与内存泄漏
【4月更文挑战第16天】本文探讨了Java的内存管理机制,重点在于垃圾回收和内存泄漏。垃圾回收通过标记-清除过程回收无用对象,Java提供了多种GC类型,如Serial、Parallel、CMS和G1。内存泄漏导致内存无法释放,常见原因包括静态集合、监听器、内部类、未关闭资源和缓存。内存泄漏影响性能,可能导致应用崩溃。避免内存泄漏的策略包括代码审查、使用分析工具、合理设计和及时释放资源。理解这些原理对开发高性能Java应用至关重要。
|
14天前
|
Java 调度 开发者
Java 21时代的标志:虚拟线程带来的并发编程新境界
Java 21时代的标志:虚拟线程带来的并发编程新境界
19 0