欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在java代码中获取JVM参数的方法

程序员文章站 2024-03-08 17:34:22
实例如下: memorymxbean memorymbean = managementfactory.getmemorymxbean(); memory...

实例如下:

memorymxbean memorymbean = managementfactory.getmemorymxbean();  
  memoryusage usage = memorymbean.getheapmemoryusage();  
  system.out.println("init heap: " + usage.getinit());  
  system.out.println("max heap: " + usage.getmax());  
  system.out.println("use heap: " + usage.getused());  
  system.out.println("\nfull information:");  
  system.out.println("heap memory usage: "  
  + memorymbean.getheapmemoryusage());  
  system.out.println("non-heap memory usage: "  
  + memorymbean.getnonheapmemoryusage());  
   
  list<string> inputarguments = managementfactory.getruntimemxbean().getinputarguments();  
  system.out.println("===================java options=============== "); 
  system.out.println(inputarguments); 
 
   
   
  system.out.println("=======================通过java来获取相关系统状态============================ "); 
  int i = (int)runtime.getruntime().totalmemory()/1024;//java 虚拟机中的内存总量,以字节为单位 
  system.out.println("总的内存量 i is "+i); 
  int j = (int)runtime.getruntime().freememory()/1024;//java 虚拟机中的空闲内存量 
  system.out.println("空闲内存量 j is "+j); 
  system.out.println("最大内存量 is "+runtime.getruntime().maxmemory()/1024); 
 
  system.out.println("=======================operatingsystemmxbean============================ "); 
  operatingsystemmxbean osm = (operatingsystemmxbean) managementfactory.getoperatingsystemmxbean(); 
//  system.out.println(osm.getfreeswapspacesize()/1024); 
//  system.out.println(osm.getfreephysicalmemorysize()/1024); 
//  system.out.println(osm.gettotalphysicalmemorysize()/1024); 
   
  //获取操作系统相关信息 
  system.out.println("osm.getarch() "+osm.getarch()); 
  system.out.println("osm.getavailableprocessors() "+osm.getavailableprocessors()); 
  //system.out.println("osm.getcommittedvirtualmemorysize() "+osm.getcommittedvirtualmemorysize()); 
  system.out.println("osm.getname() "+osm.getname()); 
  //system.out.println("osm.getprocesscputime() "+osm.getprocesscputime()); 
  system.out.println("osm.getversion() "+osm.getversion()); 
  //获取整个虚拟机内存使用情况 
  system.out.println("=======================memorymxbean============================ "); 
  memorymxbean mm=(memorymxbean)managementfactory.getmemorymxbean(); 
  system.out.println("getheapmemoryusage "+mm.getheapmemoryusage()); 
  system.out.println("getnonheapmemoryusage "+mm.getnonheapmemoryusage()); 
  //获取各个线程的各种状态,cpu 占用情况,以及整个系统中的线程状况 
  system.out.println("=======================threadmxbean============================ "); 
  threadmxbean tm=(threadmxbean)managementfactory.getthreadmxbean(); 
  system.out.println("getthreadcount "+tm.getthreadcount()); 
  system.out.println("getpeakthreadcount "+tm.getpeakthreadcount()); 
  system.out.println("getcurrentthreadcputime "+tm.getcurrentthreadcputime()); 
  system.out.println("getdaemonthreadcount "+tm.getdaemonthreadcount()); 
  system.out.println("getcurrentthreadusertime "+tm.getcurrentthreadusertime()); 
   
  //当前编译器情况 
  system.out.println("=======================compilationmxbean============================ "); 
  compilationmxbean gm=(compilationmxbean)managementfactory.getcompilationmxbean(); 
  system.out.println("getname "+gm.getname()); 
  system.out.println("gettotalcompilationtime "+gm.gettotalcompilationtime()); 
   
  //获取多个内存池的使用情况 
  system.out.println("=======================memorypoolmxbean============================ "); 
  list<memorypoolmxbean> mpmlist=managementfactory.getmemorypoolmxbeans(); 
  for(memorypoolmxbean mpm:mpmlist){ 
    system.out.println("getusage "+mpm.getusage()); 
    system.out.println("getmemorymanagernames "+mpm.getmemorymanagernames().tostring()); 
  } 
  //获取gc的次数以及花费时间之类的信息 
  system.out.println("=======================memorypoolmxbean============================ "); 
  list<garbagecollectormxbean> gcmlist=managementfactory.getgarbagecollectormxbeans(); 
  for(garbagecollectormxbean gcm:gcmlist){ 
    system.out.println("getname "+gcm.getname()); 
    system.out.println("getmemorypoolnames "+gcm.getmemorypoolnames()); 
  } 
  //获取运行时信息 
  system.out.println("=======================runtimemxbean============================ "); 
  runtimemxbean rmb=(runtimemxbean)managementfactory.getruntimemxbean(); 
  system.out.println("getclasspath "+rmb.getclasspath()); 
  system.out.println("getlibrarypath "+rmb.getlibrarypath()); 
  system.out.println("getvmversion "+rmb.getvmversion()); 

以上这篇在java代码中获取jvm参数的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。