Java直接內存泄漏排查工具gperftools使用方法

安裝編譯器
sudo yum -y install gcc make
sudo yum -y install gcc gcc-c++
安裝libunwind與google-perfile
#安裝libunwind
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-beta.tar.gz
tar -zxvf libunwind-0.99-beta.tar.gz
cd libunwind-0.99
./configure  --prefix=/data/google-perftools/libunwind  #自定義安裝目錄
make && make install

#安裝google-perfile
wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.5/gperftools-2.5.tar.gz
tar -zxvf gperftools-2.5.tar.gz
cd gperftools-2.5
./configure  --prefix=/data/google-perftools/gperftools-2.5  #自定義安裝目錄
make && make install
使配置生效
vim /etc/ld.so.conf.d/usr_local_lib.conf
/data/google-perftools/libunwind/lib  #${libunwind自定義目錄}/bin
:wq! #保存退出
/sbin/ldconfig  #執行此命令,使libunwind生效,需要sudo權限
在要監視的應用啓動前添加系統變量
export LD_PRELOAD=/data/server/google-perftools/gperftools-2.5/lib/libtcmalloc.so
export HEAPPROFILE=/data/server/google-perftools/heap/hprof  #分析文件存放位置

注意:不要加入系統變量文件中,如果加入就會導致所有使用內存的程序都會分析,導致後續操作無法進行。

執行要分析的程序
[root@master ~]# java -jar spring-boot-base-2.x-1.0-SNAPSHOT.jar --server.port=9090
Starting tracking the heap

 .   ____         _           __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.1.3.RELEASE)

4312 [main] INFO c.z.s.b.b.SpringBootBaseApplication  - Starting SpringBootBaseApplication on master with PID 54825 (/root/spring-boot-base-2.x-1.0-SNAPSHOT.jar started by root in /root) 
4338 [main] INFO c.z.s.b.b.SpringBootBaseApplication  - The following profiles are active: dev 
13786 [main] INFO o.s.b.w.e.tomcat.TomcatWebServer  - Tomcat initialized with port(s): 9090 (http) 
13892 [main] INFO o.a.coyote.http11.Http11NioProtocol  - Initializing ProtocolHandler ["http-nio-9090"] 
13937 [main] INFO o.a.catalina.core.StandardService  - Starting service [Tomcat] 
13938 [main] INFO o.a.catalina.core.StandardEngine  - Starting Servlet engine: [Apache Tomcat/9.0.16] 
13994 [main] INFO o.a.c.core.AprLifecycleListener  - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 
14350 [main] INFO o.a.c.c.C.[Tomcat].[localhost].[/]  - Initializing Spring embedded WebApplicationContext 
14351 [main] INFO o.s.web.context.ContextLoader  - Root WebApplicationContext: initialization completed in 9554 ms 
Dumping heap profile to /data/google-perftools/heap/hprof.0001.heap (1024 MB allocated cumulatively, 76 MB currently in use)
17685 [main] INFO o.s.s.c.ThreadPoolTaskExecutor  - Initializing ExecutorService 'applicationTaskExecutor' 

注意:啓動日誌中出現 Starting tracking the heapDumping heap profile to /data/google-perftools/heap/hprof.0001.heap (1024 MB allocated cumulatively, 76 MB currently in use)等字樣代表監測成功

分析結果
% /data/google-perftools/gperftools-2.5/bin/pprof -text /data/server/java/bin/java /data/google-perftools/heap/*.heap
Total: 130.5 MB
   122.3  93.8%  93.8%    122.3  93.8% os::malloc@9019e0
     6.2   4.8%  98.5%      6.2   4.8% inflateInit2_@b6d0
     1.0   0.7%  99.3%      1.0   0.7% readCEN
     0.4   0.3%  99.6%      0.4   0.3% init
     0.2   0.2%  99.7%      0.2   0.2% updatewindow
     0.1   0.1%  99.8%      0.1   0.1% _GLOBAL__sub_I_eh_alloc.cc
     0.1   0.1%  99.9%      6.3   4.8% Java_java_util_zip_Inflater_init
     0.0   0.0%  99.9%      0.0   0.0% _dl_new_object
     0.0   0.0% 100.0%      0.0   0.0% __GI__dl_allocate_tls
     0.0   0.0% 100.0%      0.0   0.0% os::malloc@901840
     0.0   0.0% 100.0%      0.0   0.0% _dl_check_map_versions
     0.0   0.0% 100.0%      0.0   0.0% read_alias_file
     0.0   0.0% 100.0%      0.0   0.0% _nl_intern_locale_data
   ...
  • The first column contains the direct memory use in MB.
    • 第一列顯示內存使用情況,英文說直接內存,暫不確定是否爲堆外直接內存
  • The fourth column contains memory use by the procedure and all of its callees.
    • 第四列包含所有調用的內存使用情況
  • The second and fifth columns are just percentage representations of the numbers in the first and fourth columns.
    • 第二列和第五列是第一列和第四列的百分比值
  • The third column is a cumulative sum of the second column (i.e., the kth entry in the third column is the sum of the first k entries in the second column.)
    • 第三列是第二列的累加
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章