JAVA企业级应用Tomcat实战
tomcat简介+安装+调优 以下的url写的很详细
http://blog.oldboyedu.com/java-tomcat/
tomcat检查排错工具
查看fgc次数
#jstat -gcutil pid 1000
[root@iZ2zehet7zctyv30e767syZ conf]# jstat -gcutil 29748 1000
S0 S1 E O P YGC YGCT FGC FGCT GCT
12.55 0.00 22.01 21.06 3.18 18 2.036 62 250.969 253.006
12.55 0.00 22.01 21.06 3.18 18 2.036 62 250.969 253.006
12.55 0.00 22.01 21.06 3.18 18 2.036 62 250.969 253.006
FGC: FGC的次数 这个值很大的话 会导致cpu负载升高 需要查看是否有内存泄漏
FGCT: FGC的时间
查看年轻带,老年代,持久带的内存分配和使用情况
#jmap -heap pid
[root@iZ2zehet7zctyv30e767syZ conf]# jmap -heap 29748 Attaching to process ID 29748, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.75-b04 using thread-local object allocation. Parallel GC with 8 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 2147483648 (2048.0MB) NewSize = 1310720 (1.25MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) NewRatio = 2 SurvivorRatio = 8 PermSize = 1073741824 (1024.0MB) MaxPermSize = 1073741824 (1024.0MB) G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: #新生代 capacity = 239075328 (228.0MB) used = 57453560 (54.79198455810547MB) free = 181621768 (173.20801544189453MB) 24.03157217460766% used From Space: capacity = 238551040 (227.5MB) used = 29933776 (28.547073364257812MB) free = 208617264 (198.9529266357422MB) 12.548164116157281% used To Space: capacity = 238551040 (227.5MB) used = 0 (0.0MB) free = 238551040 (227.5MB) 0.0% used PS Old Generation #年老带 capacity = 901775360 (860.0MB) used = 189952728 (181.15303802490234MB) free = 711822632 (678.8469619750977MB) 21.06430674708167% used PS Perm Generation #持久带 capacity = 1073741824 (1024.0MB) used = 34181336 (32.597862243652344MB) free = 1039560488 (991.4021377563477MB) 3.183384984731674% used
介绍三个代的博客
http://blog.163.com/bobile45@126/blog/static/960619922015104113740469/
tomcat其他拍错检查工具
http://blog.csdn.net/u013516966/article/details/76283793
使用Eclipse Memory Analyzer分析Tomcat内存溢出
eclipse下载地址:点击 downLoad 64bit即可
1.配置tomcat,当出现内存溢出时生成dump快照
catalina.sh 增加如下配置 JAVA_OPTS='-XX:HeapDumpPath=/home/qiqi/memerylog'
2. 安装eclipse 并安装插件Memory Analyzer
安装eclipse的时候需要先安装jdk,并配置好环境变量
安装插件方法如下:
Help-Eclipse Marketplace- 在find的位置输入“Memory Analyzer”-然后搜素 会出现这个插件-点击install 安装即可-提示restart now 重启软件 重启即可
打开dump快照的方法:
file-open file - 选择dump快照(java_pid29748.hprof)-打开
**快照通常会比较大,耗费内存和cpu,如果eclipse打不开 就进入其安装目录 修改 eclipse里的内存配置 eplipse.ini文件
打开后如下图所示 是一个饼图
点击占用内存较大位置的饼图,查看对应的类,截图给开发,运维的工作完成。
cpu负载过高,就重启tomcat的脚本
有时候一时半刻解决不了,只能当cpu负载过高的时候重启一下tomcat 脚本内容如下:
#!/bin/bash #created by qiqi 20170808 #description:when cpu load too high,restart tomcat source /etc/profile nowtime=`date +%Y-%m-%d-%H:%M:%S` # 查找平均负载 load_num=`/usr/bin/uptime | awk '{print $11}' | awk -F . '{print $1}'` #echo $load_num if [ ${load_num} -ge 4 ] then echo "进行重启操作 time:${nowtime}" pid=`ps -ef | grep tomcat | grep '/data/tomcat/temp' | grep -v grep | awk '{print $2}'` echo "the tomcat pid is $pid" if [ -n "$pid" ];then sleep 1 pid=`ps -ef | grep tomcat | grep '/data/tomcat/temp' | grep -v grep | awk '{print $2}'` if [ -n "$pid" ]; then sleep 1 echo "tomcat进程将被杀死." kill -9 $pid fi sleep 1 echo "tomcat进程已经被杀死,先重新启动tomcat." /data/tomcat/bin/startup.sh else echo "tomcat进程不存在,先重新启动tomcat." /data/tomcat/bin/startup.sh fi else echo "load:${load_num},time:${nowtime}" fi echo "<<=========================================================>>"
添加到计划任务:
#每10分钟检测一次 cpu的负载情况
*/10 * * * * /home/qiqi/restart_tomcat.sh >> /home/qiqi/log
日志显示如下:
load:0,time:2018-03-14-20:30:01 <<=========================================================>> load:0,time:2018-03-14-20:40:01 <<=========================================================>> load:0,time:2018-03-14-20:50:01 <<=========================================================>> load:0,time:2018-03-14-21:00:01 <<=========================================================>> load:0,time:2018-03-14-21:10:01 <<=========================================================>> load:0,time:2018-03-14-21:20:01 <<=========================================================>> load:0,time:2018-03-14-21:30:01 <<=========================================================>> 进行重启操作 time:2018-03-14-21:40:01 the tomcat pid is 7929 tomcat进程将被杀死. tomcat进程已经被杀死,先重新启动tomcat. Tomcat started. <<=========================================================>> load:0,time:2018-03-14-21:50:01 <<=========================================================>> load:0,time:2018-03-14-22:00:01