`
cuker919
  • 浏览: 89236 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

查看TOMCAT内存使用情况(总结)

 
阅读更多

您可以用把下列代码放在一个JSP文件中,如写入memory.jsp,放到你的TOMCAT下的任何一应用中,就可以看到你的TOMCAT总大可使用多少内存,已经使用了多少.

<%--
Document : memory
Created on : 2009-4-9, 1:35:17
Author : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JVM memory</title>
</head>
<body>
<%
double total = (Runtime.getRuntime().totalMemory()) / (1024.0 * 1024);
double max = (Runtime.getRuntime().maxMemory()) / (1024.0 * 1024);
double free = (Runtime.getRuntime().freeMemory()) / (1024.0 * 1024);
out.println("Java 虚拟机试图使用的最大内存量(当前JVM的最大可用内存)maxMemory(): " + max + "MB<br/>");
out.println("Java 虚拟机中的内存总量(当前JVM占用的内存总数)totalMemory(): " + total + "MB<br/>");
out.println("Java 虚拟机中的空闲内存量(当前JVM空闲内存)freeMemory(): " + free + "MB<br/>");
out.println("因为JVM只有在需要内存时才占用物理内存使用,所以freeMemory()的值一般情况下都很小,<br/>" +
"而JVM实际可用内存并不等于freeMemory(),而应该等于 maxMemory()-totalMemory()+freeMemory()。<br/>");
out.println("JVM实际可用内存: " + (max - total + free) + "MB<br/>");
out.println("jspcn");
%>
</body>

</html>


上面的例子基本可以满足性能调试使用,下面的比较详细,需要开发代码,暂时使用上面简单例子。


---------------------------------------------------------

同样也可以进入tomcat管理页面,查看内存使用情况,但是平常在生产环境都禁掉了管理员登录页面。

http://zhumeng8337797.blog.163.com/blog/static/10076891420129223928915/


1. Tomcat6中没有设置任何默认用户,因而需要手动往Tomcat6conf文件夹下的tomcat-users.xml文件中添加用户。

如:<role rolename="manager"/>
<user username="tomcat" password="tomcat" roles="manager"/>

注:添加完需要重启Tomcat6

2. 访问http://localhost:8080/manager/status,输入上面添加的用户名和密码。

3. 然后在如下面的JVM下可以看到内存的使用情况。
JVM:Free memory: 2.50 MB Total memory: 15.53 MB Max memory: 63.56 MB

Free memory:当前可用的内存;

Total memory:当前已经分配的JVM内存;

Max memory:当前允许分配的最大JVM内存;

上周的题目有些简单,但是tomcat也需要我们研究一下,因为涉及的知识不多,但是将来这些都是部署时候非常有用的技术。



-------------------------------------------------------

java不用jni,也可以获得当前系统性能信息

http://kakaluyi.iteye.com/blog/211492

最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接库了,但后来发现可以像下面这样做,不去调用jni,这样省去了很多看新技术的时间o(∩_∩)o...

在Java中,可以获得总的物理内存、剩余的物理内存、已使用的物理内存等信息,下面例子可以取得这些信息,并且获得在Windows下的内存使用率。
首先编写一个MonitorInfoBean类,用来装载监控的一些信息,包括物理内存、剩余的物理内存、已使用的物理内存、内存使用率等字段,该类的代码如下:

Java代码 收藏代码
  1. packagecom.amgkaka.performance;
  2. /***//**
  3. *监视信息的JavaBean类.
  4. *@authoramg
  5. *@version1.0
  6. *Creationdate:2008-4-25-上午10:37:00
  7. */
  8. publicclassMonitorInfoBean{
  9. /***//**可使用内存.*/
  10. privatelongtotalMemory;
  11. /***//**剩余内存.*/
  12. privatelongfreeMemory;
  13. /***//**最大可使用内存.*/
  14. privatelongmaxMemory;
  15. /***//**操作系统.*/
  16. privateStringosName;
  17. /***//**总的物理内存.*/
  18. privatelongtotalMemorySize;
  19. /***//**剩余的物理内存.*/
  20. privatelongfreePhysicalMemorySize;
  21. /***//**已使用的物理内存.*/
  22. privatelongusedMemory;
  23. /***//**线程总数.*/
  24. privateinttotalThread;
  25. /***//**cpu使用率.*/
  26. privatedoublecpuRatio;
  27. publiclonggetFreeMemory(){
  28. returnfreeMemory;
  29. }
  30. publicvoidsetFreeMemory(longfreeMemory){
  31. this.freeMemory=freeMemory;
  32. }
  33. publiclonggetFreePhysicalMemorySize(){
  34. returnfreePhysicalMemorySize;
  35. }
  36. publicvoidsetFreePhysicalMemorySize(longfreePhysicalMemorySize){
  37. this.freePhysicalMemorySize=freePhysicalMemorySize;
  38. }
  39. publiclonggetMaxMemory(){
  40. returnmaxMemory;
  41. }
  42. publicvoidsetMaxMemory(longmaxMemory){
  43. this.maxMemory=maxMemory;
  44. }
  45. publicStringgetOsName(){
  46. returnosName;
  47. }
  48. publicvoidsetOsName(StringosName){
  49. this.osName=osName;
  50. }
  51. publiclonggetTotalMemory(){
  52. returntotalMemory;
  53. }
  54. publicvoidsetTotalMemory(longtotalMemory){
  55. this.totalMemory=totalMemory;
  56. }
  57. publiclonggetTotalMemorySize(){
  58. returntotalMemorySize;
  59. }
  60. publicvoidsetTotalMemorySize(longtotalMemorySize){
  61. this.totalMemorySize=totalMemorySize;
  62. }
  63. publicintgetTotalThread(){
  64. returntotalThread;
  65. }
  66. publicvoidsetTotalThread(inttotalThread){
  67. this.totalThread=totalThread;
  68. }
  69. publiclonggetUsedMemory(){
  70. returnusedMemory;
  71. }
  72. publicvoidsetUsedMemory(longusedMemory){
  73. this.usedMemory=usedMemory;
  74. }
  75. publicdoublegetCpuRatio(){
  76. returncpuRatio;
  77. }
  78. publicvoidsetCpuRatio(doublecpuRatio){
  79. this.cpuRatio=cpuRatio;
  80. }
  81. }

接着编写一个获得当前的监控信息的接口,该类的代码如下所示:

Java代码 收藏代码
  1. packagecom.amgkaka.performance;
  2. /***//**
  3. *获取系统信息的业务逻辑类接口.
  4. *@authoramg*@version1.0
  5. *Creationdate:2008-3-11-上午10:06:06
  6. */
  7. publicinterfaceIMonitorService{
  8. /***//**
  9. *获得当前的监控对象.
  10. *@return返回构造好的监控对象
  11. *@throwsException
  12. *@authoramgkaka
  13. *Creationdate:2008-4-25-上午10:45:08
  14. */
  15. publicMonitorInfoBeangetMonitorInfoBean()throwsException;
  16. }

该类的实现类MonitorServiceImpl如下所示:

Java代码 收藏代码
  1. packagecom.amgkaka.performance;
  2. importjava.io.InputStreamReader;
  3. importjava.io.LineNumberReader;
  4. importsun.management.ManagementFactory;
  5. importcom.sun.management.OperatingSystemMXBean;
  6. /***//**
  7. *获取系统信息的业务逻辑实现类.
  8. *@authoramg*@version1.0Creationdate:2008-3-11-上午10:06:06
  9. */
  10. publicclassMonitorServiceImplimplementsIMonitorService{
  11. //可以设置长些,防止读到运行此次系统检查时的cpu占用率,就不准了
  12. privatestaticfinalintCPUTIME=5000;
  13. privatestaticfinalintPERCENT=100;
  14. privatestaticfinalintFAULTLENGTH=10;
  15. /***//**
  16. *获得当前的监控对象.
  17. *@return返回构造好的监控对象
  18. *@throwsException
  19. *@authoramg*Creationdate:2008-4-25-上午10:45:08
  20. */
  21. publicMonitorInfoBeangetMonitorInfoBean()throwsException{
  22. intkb=1024;
  23. //可使用内存
  24. longtotalMemory=Runtime.getRuntime().totalMemory()/kb;
  25. //剩余内存
  26. longfreeMemory=Runtime.getRuntime().freeMemory()/kb;
  27. //最大可使用内存
  28. longmaxMemory=Runtime.getRuntime().maxMemory()/kb;
  29. OperatingSystemMXBeanosmxb=(OperatingSystemMXBean)ManagementFactory
  30. .getOperatingSystemMXBean();
  31. //操作系统
  32. StringosName=System.getProperty("os.name");
  33. //总的物理内存
  34. longtotalMemorySize=osmxb.getTotalPhysicalMemorySize()/kb;
  35. //剩余的物理内存
  36. longfreePhysicalMemorySize=osmxb.getFreePhysicalMemorySize()/kb;
  37. //已使用的物理内存
  38. longusedMemory=(osmxb.getTotalPhysicalMemorySize()-osmxb
  39. .getFreePhysicalMemorySize())
  40. /kb;
  41. //获得线程总数
  42. ThreadGroupparentThread;
  43. for(parentThread=Thread.currentThread().getThreadGroup();parentThread
  44. .getParent()!=null;parentThread=parentThread.getParent())
  45. ;
  46. inttotalThread=parentThread.activeCount();
  47. doublecpuRatio=0;
  48. if(osName.toLowerCase().startsWith("windows")){
  49. cpuRatio=this.getCpuRatioForWindows();
  50. }
  51. //构造返回对象
  52. MonitorInfoBeaninfoBean=newMonitorInfoBean();
  53. infoBean.setFreeMemory(freeMemory);
  54. infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
  55. infoBean.setMaxMemory(maxMemory);
  56. infoBean.setOsName(osName);
  57. infoBean.setTotalMemory(totalMemory);
  58. infoBean.setTotalMemorySize(totalMemorySize);
  59. infoBean.setTotalThread(totalThread);
  60. infoBean.setUsedMemory(usedMemory);
  61. infoBean.setCpuRatio(cpuRatio);
  62. returninfoBean;
  63. }
  64. /***//**
  65. *获得CPU使用率.
  66. *@return返回cpu使用率
  67. *@authoramg*Creationdate:2008-4-25-下午06:05:11
  68. */
  69. privatedoublegetCpuRatioForWindows(){
  70. try{
  71. StringprocCmd=System.getenv("windir")
  72. +"\\system32\\wbem\\wmic.exeprocessgetCaption,CommandLine,"
  73. +"KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
  74. //取进程信息
  75. long[]c0=readCpu(Runtime.getRuntime().exec(procCmd));
  76. Thread.sleep(CPUTIME);
  77. long[]c1=readCpu(Runtime.getRuntime().exec(procCmd));
  78. if(c0!=null&&c1!=null){
  79. longidletime=c1[0]-c0[0];
  80. longbusytime=c1[1]-c0[1];
  81. returnDouble.valueOf(
  82. PERCENT*(busytime)/(busytime+idletime))
  83. .doubleValue();
  84. }else{
  85. return0.0;
  86. }
  87. }catch(Exceptionex){
  88. ex.printStackTrace();
  89. return0.0;
  90. }
  91. }
  92. /***//**
  93. *读取CPU信息.
  94. *@paramproc
  95. *@return
  96. *@authoramg*Creationdate:2008-4-25-下午06:10:14
  97. */
  98. privatelong[]readCpu(finalProcessproc){
  99. long[]retn=newlong[2];
  100. try{
  101. proc.getOutputStream().close();
  102. InputStreamReaderir=newInputStreamReader(proc.getInputStream());
  103. LineNumberReaderinput=newLineNumberReader(ir);
  104. Stringline=input.readLine();
  105. if(line==null||line.length()<FAULTLENGTH){
  106. returnnull;
  107. }
  108. intcapidx=line.indexOf("Caption");
  109. intcmdidx=line.indexOf("CommandLine");
  110. introcidx=line.indexOf("ReadOperationCount");
  111. intumtidx=line.indexOf("UserModeTime");
  112. intkmtidx=line.indexOf("KernelModeTime");
  113. intwocidx=line.indexOf("WriteOperationCount");
  114. longidletime=0;
  115. longkneltime=0;
  116. longusertime=0;
  117. while((line=input.readLine())!=null){
  118. if(line.length()<wocidx){
  119. continue;
  120. }
  121. //字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
  122. //ThreadCount,UserModeTime,WriteOperation
  123. Stringcaption=Bytes.substring(line,capidx,cmdidx-1)
  124. .trim();
  125. Stringcmd=Bytes.substring(line,cmdidx,kmtidx-1).trim();
  126. if(cmd.indexOf("wmic.exe")>=0){
  127. continue;
  128. }
  129. //log.info("line="+line);
  130. if(caption.equals("SystemIdleProcess")
  131. ||caption.equals("System")){
  132. idletime+=Long.valueOf(
  133. Bytes.substring(line,kmtidx,rocidx-1).trim())
  134. .longValue();
  135. idletime+=Long.valueOf(
  136. Bytes.substring(line,umtidx,wocidx-1).trim())
  137. .longValue();
  138. continue;
  139. }
  140. kneltime+=Long.valueOf(
  141. Bytes.substring(line,kmtidx,rocidx-1).trim())
  142. .longValue();
  143. usertime+=Long.valueOf(
  144. Bytes.substring(line,umtidx,wocidx-1).trim())
  145. .longValue();
  146. }
  147. retn[0]=idletime;
  148. retn[1]=kneltime+usertime;
  149. returnretn;
  150. }catch(Exceptionex){
  151. ex.printStackTrace();
  152. }finally{
  153. try{
  154. proc.getInputStream().close();
  155. }catch(Exceptione){
  156. e.printStackTrace();
  157. }
  158. }
  159. returnnull;
  160. }
  161. /***//**
  162. *测试方法.
  163. *@paramargs
  164. *@throwsException
  165. *@authoramg*Creationdate:2008-4-30-下午04:47:29
  166. */
  167. publicstaticvoidmain(String[]args)throwsException{
  168. IMonitorServiceservice=newMonitorServiceImpl();
  169. MonitorInfoBeanmonitorInfo=service.getMonitorInfoBean();
  170. System.out.println("cpu占有率="+monitorInfo.getCpuRatio());
  171. System.out.println("可使用内存="+monitorInfo.getTotalMemory());
  172. System.out.println("剩余内存="+monitorInfo.getFreeMemory());
  173. System.out.println("最大可使用内存="+monitorInfo.getMaxMemory());
  174. System.out.println("操作系统="+monitorInfo.getOsName());
  175. System.out.println("总的物理内存="+monitorInfo.getTotalMemorySize()+"kb");
  176. System.out.println("剩余的物理内存="+monitorInfo.getFreeMemory()+"kb");
  177. System.out.println("已使用的物理内存="+monitorInfo.getUsedMemory()+"kb");
  178. System.out.println("线程总数="+monitorInfo.getTotalThread()+"kb");
  179. }
  180. }

该实现类中需要用到一个自己编写byte的工具类,该类的代码如下所示:

Java代码 收藏代码
  1. packagecom.amgkaka.performance;
  2. /***//**
  3. *byte操作类.
  4. *@authoramg*@version1.0
  5. *Creationdate:2008-4-30-下午04:57:23
  6. */
  7. publicclassBytes{
  8. /***//**
  9. *由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在
  10. *包含汉字的字符串时存在隐患,现调整如下:
  11. *@paramsrc要截取的字符串
  12. *@paramstart_idx开始坐标(包括该坐标)
  13. *@paramend_idx截止坐标(包括该坐标)
  14. *@return
  15. */
  16. publicstaticStringsubstring(Stringsrc,intstart_idx,intend_idx){
  17. byte[]b=src.getBytes();
  18. Stringtgt="";
  19. for(inti=start_idx;i<=end_idx;i++){
  20. tgt+=(char)b[i];
  21. }
  22. returntgt;
  23. }
  24. }

运行下MonitorBeanImpl类,读者将会看到当前的内存、cpu利用率等信息。


分享到:
评论

相关推荐

    tomcat内存溢出总结

    tomcat内存溢出总结 在生产环境中tomcat内存设置不好很容易出现内存溢出。造成内存原因是不一样的,当然处理方式也不一样。 这里根据平时遇到的情况和相关资料进行一个总结。常见的一般会有下面三种情况: 1....

    扩大Tomcat内存

    现总结有以下几种扩大Tomcat内存的方法可以选用: 方法一:这种方法是在Tomcat使用startup.bat文件启动项目的情况下,在Tomcat文件下找到“/bin/catalina.bat”,在catalina.bat的第一行增加: set JAVA_OPTS= -Xms...

    tomcat内存溢出总结(2013年)

    tomcat内存溢出总结:堆溢出。永久保存区域溢出 等等各种常见内存溢出问题的解决方法

    Tomcat内存溢出的三种情况及解决办法分析

    Tomcat内存溢出的三种情况及解决办法分析 Tomcat内存溢出的原因 在生产环境中tomcat内存设置不好很容易出现内存溢出。造成内存原因是不一样的,当然处理方式也不一样。 这里根据平时遇到的情况和相关资料进行一个...

    tomcat内存溢出解决办法

    根据网上总结tomcat内存溢出的各种解决办法,很实用!

    tomcat性能调优总结

    在Tomcat和应用程序进行了压力测试后,如果您对应用程序的性能结果不太满意,就可以采取一些性能调整措施了,当然了前提是应用程序没有问题,tomcat性能调优总结,欢迎下载

    tomcat内存溢出

    tomcat内存溢出 问题 自己总结的 非常好用

    Tomcat加载到虚拟内存、端口设置、内存加大

    文档是自己在使用过程中总结的,有什么不足之处,还请谅解。

    tomcat溢出解决建议方案

    在windows server2003操作系统(32bit)下运行tomcat的web App经常发生内存溢出,其规律是发生溢出后重新启动tomcat,再过一个多月仍旧发生,一般来说,造成内存溢出原因是不一样的,当然处理方式也不一样。...

    Linux服务器上的Tomcat进程频繁被杀

    目前服务器上的Tomcat服务经常出现宕机的情况,想通过查看系统日志/var/log/messages来确定问题,但是系统日志没有开启记录,故执行以下语句重启日志服务 sudo /etc/init.d/rsyslog restart 等待下一次的问题出现后,...

    java深入学习教程书籍ppt及pdf集合

    java反射机制总结pdf java数据结构上机实践指导教程pdf java网络编程pdf jvm内存问题最佳实践ppt jvm实现机制ppt jvm调优word tomcat详细资料word tomcat源码研究 java类库简介和数据结构类使用ppt 深入java虚拟机...

    java 面试题 总结

    但通常情况下,由于Java Bean是被容器所创建(如Tomcat)的,所以Java Bean应具有一个无参的构造器,另外,通常Java Bean还要实现Serializable接口用于实现Bean的持久性。Java Bean实际上相当于微软COM模型中的本地...

    Java项目:CRM人事管理系统(java+SSM+JSP+Layui+jQuery+mysql)

    3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.是否Maven项目: 是;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目 6.数据库:...

    Eclipse开发分布式商城系统+完整视频代码及文档

    │ │ 深入理解Java内存模型.pdf │ │ │ └─课后资料 │ ├─笔记 │ │ 淘淘商城_day20_课堂笔记.docx │ │ │ └─视频 │ 07-使用Jedis连接集群操作.avi │ 00-今日大纲.avi │ 01-RDB持久化方式.avi │ 02...

    jvm调优思维脑图

    主要包括:java内存结构、堆内存的构成、堆内存参数调整、垃圾收集算法、垃圾收集器选择、JVM参数、java对象的内存分配过程、Tomcat调优并使用Jmeter评测、参考资料等。希望对大家学习jvm有帮助。

    Java项目:教师师资管理系统(java+SSM+JSP+bootstrap+Mysql)

    3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7/8.0版本均可; 6.是否Maven项目:是; 技术栈 1. 后端:Spring+SpringMVC+Mybatis 2. ...

Global site tag (gtag.js) - Google Analytics