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.)(第三列是第二列的累加)

你可能感兴趣的:(Java直接内存泄漏排查工具gperftools使用方法)