博客主页:http://blog.****.net/minna_d
性能调优时结果通常需要保证结果的一致性才有意义。
为了保证结果的一致性,必须了解到已下命令, 屏蔽机器、缓存等一系列外围因素
1. df -h 查看磁盘挂载点及内存使用情况
[root@jjy ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 97G 63G 30G 69% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/sdb 459G 36G 400G 9% /notSolidHd
2. 测试磁盘读写速度
[root@jjy ~]# hdparm -t /dev/sda3
/dev/sda3:
Timing buffered disk reads: 2
448 MB in 3.03 seconds = 147.85 MB/sec
3.查看物理内存使用情况
[root@jjy ~]# free -m
total used free shared buffers cached
Mem: 7842 1008 6833 0 2 426
-/+ buffers/cache: 579 7263
Swap: 3906 0 3906
4. 清空系统缓存
xxxxxxxxxxxxxx: free -m
total used free shared buffers cached
Mem: 8192 8162 29 0 139 2519
-/+ buffers/cache: 5503 2688
Swap: 4094 2642 1451
echo 1 > /proc/sys/vm/drop_caches
xxxxxxxxxxxxxx: free -m
total used free shared buffers cached
Mem: 8192 6105 2086 0 1 592
-/+ buffers/cache: 5511 2680
Swap: 4094 2642 1451
0 – 不释放
1 – 释放页缓存,清空最近访问过的文件页面缓存
2 – 释放dentries和inodes,清空文件节点缓存和目录项缓存
3 – 释放所有缓存,清空1和2所有内容的缓存
5. top 命令, 查看内存, cpu使用情况, 不过对于java程序, 注意res 与 virt 项, 代表的是最大内存容量和常驻内存量,
并不是GC之后最低内存量,内存优化之后通常看的不是top ,而是fullgc 之后内存量或者用下一个jmap命令。
top - 11:06:44 up 964 days, 20:12, 1 user, load average: 0.06, 0.04, 0.00
Tasks: 143 total, 1 running, 142 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.2%us, 0.0%sy, 0.0%ni, 99.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.3%st
Mem: 8388608k total, 6252204k used, 2136404k free, 2688k buffers
Swap: 4192924k total, 2706288k used, 1486636k free, 618396k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
21874 tomcat 18 0 8832m 5.0g 10m S 0.0 62.6 40:10.09 java
25593 postgres 16 0 124m 26m 25m S 0.0 0.3 2080:58 postgres
4477 postgres 15 0 126m 21m 20m S 0.0 0.3 0:00.79 postgres
6. jmap -heap pid 查看jvm内存结构分布
[root@jjy ~/q/data/qtypeahead]# jmap -heap 1742
Attaching to process ID 1742, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.45-b08
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 7864320000 (7500.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 17592186044415 MB
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 21757952 (20.75MB)
MaxPermSize = 85983232 (82.0MB)
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 874512384 (834.0MB)
used = 874512384 (834.0MB)
free = 0 (0.0MB)
100.0% used
From Space:
capacity = 873463808 (833.0MB)
used = 594060480 (566.5402221679688MB)
free = 279403328 (266.45977783203125MB)
68.01203147274535% used
To Space:
capacity = 873463808 (833.0MB)
used = 289505280 (276.09375MB)
free = 583958528 (556.90625MB)
33.14450780312125% used
PS Old Generation
capacity = 5242880000 (5000.0MB)
used = 4480736448 (4273.163269042969MB)
free = 762143552 (726.8367309570312MB)
85.46326538085937% used
PS Perm Generation
capacity = 22544384 (21.5MB)
used = 22447888 (21.407974243164062MB)
free = 96496 (0.0920257568359375MB)
99.57197322401889% used
7.动态展示内存变化情况jconsole pid& ,我不喜欢截图,请自己尝试
8. jmap -histo:live pid 查看程序稳定之后内存
[root@jjy ~/q/data/qtypeahead]# jmap -histo:live 1742 > histo_live
[root@jjy ~/q/data/qtypeahead]# head histo_live && tail histo_live
num #instances #bytes class name
----------------------------------------------
1: 31395319 1336843920 [C
2: 24818078 992723120 java.util.TreeMap$Entry
3: 31394031 753456744 java.lang.String
4: 24816294 595591056 com.qunar.hotel.qtypeahead.datastructure.QElement
5: 7193675 345296400 java.util.HashMap
6: 7151274 343261152 com.qunar.hotel.qtypeahead.datastructure.QElementMin
7: 2902593 233245400 [Ljava.util.HashMap$Entry;
2427: 1 16 com.sun.jmx.remote.internal.ArrayNotificationBuffer$BroadcasterQuery
2428: 1 16 [Ljava.lang.management.MonitorInfo;
2429: 1 16 [Lorg.codehaus.jackson.map.deser.ValueInstantiators;
2430: 1 16 com.sun.org.apache.xerces.internal.dom.CharacterDataImpl$1
2431: 1 16 org.apache.http.impl.DefaultHttpResponseFactory
2432: 1 16 sun.reflect.GeneratedSerializationConstructorAccessor17
2433: 1 16 sun.nio.cs.CharsetMapping$4
2434: 1 16 com.qunar.base.meerkat.http.SSLVerifier
2435: 1 16 com.google.gson.internal.bind.TypeAdapters$12
Total 147825569 5032606128
9. 数据库也有自己的缓存, 同一条sql执行时间第一次比二次慢
hs=# \timing
Timing is on.
hs=# select count(*) from xx;
count
---------
7403942
(1 row)
Time: 6542.208 ms
hs=# select count(*) from xx;
count
---------
7403942
(1 row)
Time: 2022.684 ms
所以测试sql时最好使用explain analyze减少差异
hs=# explain analyze select count(*) from xx;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=231919.62..231919.64 rows=1 width=0) (actual time=21282.771..21282.773 rows=1 loops=1)
-> Seq Scan on qt_hotel_log_analyse_result (cost=0.00..213329.30 rows=7436130 width=0) (actual time=1.081..11185.959 rows=7403942 loops=1)
Total runtime: 21282.839 ms
(3 rows)
Time: 21302.382 ms
hs=# explain analyze select count(*) from xx;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=231919.62..231919.64 rows=1 width=0) (actual time=20388.296..20388.297 rows=1 loops=1)
-> Seq Scan on qt_hotel_log_analyse_result (cost=0.00..213329.30 rows=7436130 width=0) (actual time=0.056..10702.320 rows=7403942 loops=1)
Total runtime: 20388.353 ms
(3 rows)
Time: 20389.865 ms