1. 查找进程
top
2. 查找线程
top -H -p <pid>
3. 查找java的堆栈信息
# 将线程id转换成十六进制
printf "%x\n" <tid>
# 使用jstack查询线程的堆栈信息
jstack <pid> | grep -a 线程id(十六进制)
4. 其他
strace -p 24167(tid)
1. 查找进程
top
2. 查找线程
top -H -p <pid>
3. 查找java的堆栈信息
# 将线程id转换成十六进制 printf "%x\n" <tid> # 使用jstack查询线程的堆栈信息 jstack <pid> | grep -a 线程id(十六进制)
4. 其他
strace -p 24167(tid)
转载于:https://my.oschina.net/ironwill/blog/1557076
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class Test { public int testCpu() { int cpuUsage = 0; String osName = System.getProperty("os.name");//查看系统类型 if (osName.toLowerCase().contains("windows") || osName.toLowerCase().contains("win")) { try { String procCmd = System.getenv("windir") + "//system32//wbem//wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount"; // 取进程信息 long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));// 第一次读取CPU信息 Thread.sleep(500);// 睡500ms long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));// 第二次读取CPU信息 if (c0 != null && c1 != null) { long idletime = c1[0] - c0[0];// 空闲时间 long busytime = c1[1] - c0[1];// 使用时间 Double cpusage = Double.valueOf(100 * (busytime) * 1.0 / (busytime + idletime)); BigDecimal b1 = new BigDecimal(cpusage); cpuUsage = b1.setScale(2, BigDecimal.ROUND_HALF_UP).intValue(); } else { cpuUsage = 0; } } catch (Exception ex) { cpuUsage = 0; } } else { try { Map<?, ?> map1 = cpuinfo(); Thread.sleep(500); Map<?, ?> map2 = cpuinfo(); long user1 = Long.parseLong(map1.get("user").toString()); long nice1 = Long.parseLong(map1.get("nice").toString()); long system1 = Long.parseLong(map1.get("system").toString()); long idle1 = Long.parseLong(map1.get("idle").toString()); long user2 = Long.parseLong(map2.get("user").toString()); long nice2 = Long.parseLong(map2.get("nice").toString()); long system2 = Long.parseLong(map2.get("system").toString()); long idle2 = Long.parseLong(map2.get("idle").toString()); long total1 = user1 + system1 + nice1; long total2 = user2 + system2 + nice2; float total = total2 - total1; long totalIdle1 = user1 + nice1 + system1 + idle1; long totalIdle2 = user2 + nice2 + system2 + idle2; float totalidle = totalIdle2 - totalIdle1; float cpusage = (total / totalidle) * 100; BigDecimal b1 = new BigDecimal(cpusage); cpuUsage = b1.setScale(2, BigDecimal.ROUND_HALF_UP).intValue(); } catch (InterruptedException e) { cpuUsage = 0; } } return cpuUsage; } private static long[] readCpu(final Process proc) { long[] retn = new long[2]; try { proc.getOutputStream().close(); InputStreamReader ir = new InputStreamReader(proc.getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line = input.readLine(); if (line == null || line.length() < 10) { return null; } int capidx = line.indexOf("Caption"); int cmdidx = line.indexOf("CommandLine"); int rocidx = line.indexOf("ReadOperationCount"); int umtidx = line.indexOf("UserModeTime"); int kmtidx = line.indexOf("KernelModeTime"); int wocidx = line.indexOf("WriteOperationCount"); long idletime = 0; long kneltime = 0; long usertime = 0; while ((line = input.readLine()) != null) { if (line.length() < wocidx) { continue; } // 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount, // ThreadCount,UserModeTime,WriteOperation String caption = substring(line, capidx, cmdidx - 1).trim(); String cmd = substring(line, cmdidx, kmtidx - 1).trim(); if (cmd.indexOf("wmic.exe") >= 0) { continue; } String s1 = substring(line, kmtidx, rocidx - 1).trim(); String s2 = substring(line, umtidx, wocidx - 1).trim(); if (caption.equals("System Idle Process") || caption.equals("System")) { if (s1.length() > 0) idletime += Long.valueOf(s1).longValue(); if (s2.length() > 0) idletime += Long.valueOf(s2).longValue(); continue; } if (s1.length() > 0) kneltime += Long.valueOf(s1).longValue(); if (s2.length() > 0) usertime += Long.valueOf(s2).longValue(); } retn[0] = idletime; retn[1] = kneltime + usertime; return retn; } catch (Exception e) { e.printStackTrace(); } finally { try { proc.getInputStream().close(); } catch (Exception e2) { e2.printStackTrace(); } } return null; } /** * * @param src * 要截取的字符串 * @param start_idx * 开始坐标(包括该坐标) * @param end_idx * 截止坐标(包括该坐标) * @return */ private static String substring(String src, int start_idx, int end_idx) { byte[] b = src.getBytes(); String tgt = ""; for (int i = start_idx; i <= end_idx; i++) { tgt += (char) b[i]; } return tgt; } /** * 功能:Linux CPU使用信息 */ public Map<?, ?> cpuinfo() { InputStreamReader inputs = null; BufferedReader buffer = null; Map<String, Object> map = new HashMap<String, Object>(); try { inputs = new InputStreamReader(new FileInputStream("/proc/stat")); buffer = new BufferedReader(inputs); String line = ""; while (true) { line = buffer.readLine(); if (line == null) { break; } if (line.startsWith("cpu")) { StringTokenizer tokenizer = new StringTokenizer(line); List<String> temp = new ArrayList<String>(); while (tokenizer.hasMoreElements()) { String value = tokenizer.nextToken(); temp.add(value); } map.put("user", temp.get(1)); map.put("nice", temp.get(2)); map.put("system", temp.get(3)); map.put("idle", temp.get(4)); map.put("iowait", temp.get(5)); map.put("irq", temp.get(6)); map.put("softirq", temp.get(7)); map.put("stealstolen", temp.get(8)); break; } } } catch (Exception e) { } finally { try { buffer.close(); inputs.close(); } catch (Exception e2) { } } return map; } }
如何处理MySQL经常出现CPU占用率达到99%
情况说明:
最近在自己购买的linux服务器上捣鼓了一个小项目,按理说不存在CPU占用率会达到100%的情况,但事实就是经常出现。
然后,我第一反应是“卧槽,被人当矿机了?”,然后一顿查询操作后,发现并没有被人捣鼓,问题出现在mysql上,MySQL的CPU占用率达到了100%;这是我就很纳闷了,这么小个程序,不应该啊。然后就开始了排查。
查询了下sql进程,发现:
mysql> show processlist;
+-----+------+----------------------+-------------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+----------------------+-------------+---------+------+-------+------------------+
| 8 | root | 182.150.55.136:64138 | itresources | Sleep | 16686 | | NULL |
| 18 | root | 182.150.55.136:65138 | itresources | Sleep | 12886 | | NULL |
| 22 | root | 182.150.55.136:66138 | itresources | Sleep | 14486 | | NULL |
| 11 | root | 182.150.55.136:67138 | itresources | Sleep | 1116 | | NULL |
| 206 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+-----+------+----------------------+-------------+---------+------+-------+------------------+
2 rows in set (0.00 sec)
发现MySQL上有大量的闲置连接。
解决措施:
MySQL服务器所支持的最大连接数是有上限的,因为每个连接的建立都会消耗内存,因此我们希望客户端在连接到MySQL Server处理完相应的操作后,应该断开连接并释放占用的内存。如果你的MySQL Server有大量的闲置连接,他们不仅会白白消耗内存,而且如果连接一直在累加而不断开,最终肯定会达到MySQL Server的连接上限数,这会报‘too many connections‘的错误。对于wait_timeout的值设定,应该根据系统的运行情况来判断。在系统运行一段时间后,可以通过show processlist命令查看当前系统的连接状态,如果发现有大量的sleep状态的连接进程,则说明该参数设置的过大,可以进行适当的调整小些。
Mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name | Value |
+-----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_flush_log_at_timeout | 1 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 28800 |
+-----------------------------+----------+
11 rows in set (0.00 sec)
经查询发现原来mysql没有进行过优化,还是原来的默认值:28800(即8个小时)
编辑 /etc/my.cnf 文件,在mysqld 下 新增 timeout参数,设置为120秒,如下:
【mysqld】
wait_timeout=120
interactive_timeout=120
注意:要同时设置interactive_timeout和wait_timeout才会生效。
最后重启一下mysql 生效 即可!
mysql> show variables like '%timeout%';
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 120 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 120 |
+----------------------------+----------+
10 rows in set (0.00 sec)
原文:https://www.cnblogs.com/mmzs/p/11239896.html
1.使用命令cat /proc/meminfo
根据以上命令可以得知:
memtotal:3881812Kb
其物理内存是3.8G
可供剩余调用的内存是
106184也就是103M:
以下就是测量一个进程在运行的时候占用了多少个内存的验证方法:
内存空间比较大了,来验证一下:
此时引用一个spoon的内存调度看一下:
验证结果如下所示: