论坛首页 综合技术论坛

用ab命令来对 JVM进行内存分析的一个例子

浏览 1631 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-09-26  

之前写在新浪博客里的,转发过来:

 

关于ab的介绍前面的帖子已经说过了。

现在就结合之前讲得JVM内存,用实例来过一遍:

1、JVM的启动参数
·我是这样设置的:
  java -Xmx1024m -Xms1024m -Xss128k -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxPermSize=16m

·启动tomcat之后,使用 jmap -heap `pgrep -u root java`,得到如下信息:
Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 1073741824 (1024.0MB)
   NewSize          = 1048576 (1.0MB)
   MaxNewSize       = 4294901760 (4095.9375MB)
   OldSize          = 4194304 (4.0MB)
   NewRatio         = 4
   SurvivorRatio    = 4
   PermSize         = 12582912 (12.0MB)
   MaxPermSize      = 16777216 (16.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 178913280 (170.625MB)
   used     = 51533904 (49.14656066894531MB)
   free     = 127379376 (121.47843933105469MB)
   28.80384508070055% used
Eden Space:
   capacity = 143130624 (136.5MB)
   used     = 51533904 (49.14656066894531MB)
   free     = 91596720 (87.35343933105469MB)
   36.00480635087569% used
From Space:
   capacity = 35782656 (34.125MB)
   used     = 0 (0.0MB)
   free     = 35782656 (34.125MB)
   0.0% used
To Space:
   capacity = 35782656 (34.125MB)
   used     = 0 (0.0MB)
   free     = 35782656 (34.125MB)
   0.0% used
tenured generation:
   capacity = 859045888 (819.25MB)
   used     = 1952984 (1.8625106811523438MB)
   free     = 857092904 (817.3874893188477MB)
   0.22734338494383202% used
Perm Generation:
   capacity = 12582912 (12.0MB)
   used     = 6656024 (6.347679138183594MB)
   free     = 5926888 (5.652320861816406MB)
   52.897326151529946% used
------------------------------------------华丽的分割线---------------------------------------

按照这个参数来计算的话(可以参考这里:http://blog.sina.com.cn/s/blog_68158ebf0100wp83.html
-Xmx1024m -Xms1024m -Xss128k -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxPermSize=16m

-Xmx1024m 最大堆内存为 1024M
-Xms1024m 初始堆内存为 1024M

-XX:NewRatio=4 
则 年轻代:年老代=1:4  1024M/5=204.8M
故 年轻代=204.8M  年老代=819.2M

-XX:SurvivorRatio=4
则年轻代中 2Survivor:1Eden=2:4 204.8M/6=34.13333333333333M
故 Eden=136.5333333333333M  1Suivivor=34.13333333333333M

用 jmap -heap <pid>
查看的结果 与我们计算的结果一致

-----------------------------------华丽的分割线-------------------------------------------

3、编写测试页面
    在网站根目录里新建页面perf.jsp,内容如下:
<%intsize = (int)(1024 * 1024 * m);byte[] buffer = new byte[size];Thread.sleep(s);%>

注:m值用来设置每次申请内存的大小,s 表示睡眠多少ms

4、使用jstat来监控内存变化
·jstat命令的用法和介绍,参考这里http://blog.sina.com.cn/s/blog_68158ebf0100woyh.html

这里使用 jstat -gcutil `pgrep -u root java` 1500 10 
   再解释一下,这里有三个参数:
·pgrep -u root java --> 得到java的进程ID号
·1500 --> 表示每隔1500ms取一次数据
·10 --> 表示一共取10次数据

5、用ab来进行压测

压测的命令:[root@CentOS ~]# ab -c150 -n50000 "http://localhost/perf.jsp?m=1&s=10"

注:这里使用150个线程并发访问,一共访问50000次。
    另外我做了apache与tomcat的整合,过程见:http://blog.sina.com.cn/s/blog_68158ebf0100wnvx.html
默认情况下你可以使用 http://localhost:8080/perf.jsp?m=1&s=10 来访问。

--------------------------------------------华丽的分割线----------------------------------------
下面开始进行实验:
·先启动Java内存的监听:
    [root@CentOS ~]# jstat -gcutil 8570 1500 10 
·在开启一个终端,开始压测:
    [root@CentOS ~]# ab -c150 -n50000 "http://localhost/perf.jsp?m=1&s=10"

两个命令结束之后的结果如下:
·jstat:
[root@CentOS ~]# jstat -gcutil 8570 1500 10 
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   
  0.06   0.00  53.15   2.03  67.18     52    0.830     1    0.218    1.048
  0.00   0.04  18.46   2.03  67.18     55    0.833     1    0.218    1.052
  0.03   0.00  28.94   2.03  67.18     56    0.835     1    0.218    1.053
  0.00   0.04  34.02   2.03  67.18     57    0.836     1    0.218    1.054
  0.04   0.00  34.13   2.03  67.18     58    0.837     1    0.218    1.055
  0.00   0.04  38.62   2.03  67.18     59    0.838     1    0.218    1.056
  0.04   0.00   8.39   2.03  67.18     60    0.839     1    0.218    1.058
  0.04   0.00   8.39   2.03  67.18     60    0.839     1    0.218    1.058
  0.04   0.00   8.39   2.03  67.18     60    0.839     1    0.218    1.058
  0.04   0.00   8.39   2.03  67.18     60    0.839     1    0.218    1.058

结果简单解析:
    可以看到JVM里S0和S1始终有一个是空的,Eden区达到一定比例之后就会产生Minor GC,由于我这里的Old Generation 区设置的比较大,所以没有产生Full GC。

·ab
[root@CentOS ~]# ab -c150 -n50000 "http://localhost/perf.jsp?m=1&s=10"

This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Finished 50000 requests


Server Software:        Apache/2.2.3
Server Hostname:        localhost
Server Port:            80

Document Path:          /perf.jsp?m=1&s=10
Document Length:        979 bytes

Concurrency Level:      150
Time taken for tests:   13.467648 seconds
Complete requests:      50000
Failed requests:        0
Write errors:           0
Non-2xx responses:      50005
Total transferred:      57605760 bytes
HTML transferred:       48954895 bytes
Requests per second:    3712.60 [#/sec] (mean)
Time per request:       40.403 [ms] (mean)   #平均请求时间
Time per request:       0.269 [ms] (mean, across all concurrent requests) 
Transfer rate:          4177.05 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  46.5      0    3701
Processing:    10   38  70.3     36    6885
Waiting:        3   35  70.3     33    6883
Total:         10   39  84.4     37    6901

Percentage of the requests served within a certain time (ms)
  50%     37
  66%     38
  75%     39
  80%     39
  90%     41
  95%     43
  98%     50
  99%     58
 100%   6901 (longest request)


------------------------------------------华丽的分割线------------------------------------------

全文完。
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics