早上,同事发来一个消息“容器镜像帮忙看下把这个包装一下,不然没法用jmap看java进程具体的内存使用情况”链接。近期,微服务中的长连接服务由于内存溢出,十分吃资源。而研发排查java内存溢出需要jmap软件,且需要debug的包。
经过几经周折,发现了安装java及相关附件的方法。

安装java

  • 基于centos7.9系统,可以直接yum install -y java-1.8.0-openjdk
  • 如果使用jmap,需要安装yum install -y java-1.8.0-openjdk-devel
  • 而使用jmap -heap ,需要yum --enablerepo=*debug* install -y java-1.8.0-openjdk-debuginfo

使用效果是这样的

sh-4.2# jmap -heap 7
Attaching to process ID 7, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.392-b08

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 2147483648 (2048.0MB)
   NewSize                  = 715653120 (682.5MB)
   MaxNewSize               = 715653120 (682.5MB)
   OldSize                  = 1431830528 (1365.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 134217728 (128.0MB)
   CompressedClassSpaceSize = 778043392 (742.0MB)
   MaxMetaspaceSize         = 786432000 (750.0MB)
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 348651520 (332.5MB)
   used     = 290366624 (276.9151916503906MB)
   free     = 58284896 (55.584808349609375MB)
   83.28276440613251% used
From Space:
   capacity = 178257920 (170.0MB)
   used     = 15302688 (14.593780517578125MB)
   free     = 162955232 (155.40621948242188MB)
   8.584576775045957% used
To Space:
   capacity = 167772160 (160.0MB)
   used     = 0 (0.0MB)
   free     = 167772160 (160.0MB)
   0.0% used
PS Old Generation
   capacity = 1431830528 (1365.5MB)
   used     = 490085312 (467.38177490234375MB)
   free     = 941745216 (898.1182250976562MB)
   34.22788538281536% used

15028 interned Strings occupying 1331304 bytes.
sh-4.2#

本以为这样就完了,没想到打印的日志是“问号?”。又是几经周折,找到了方法,而且是适用于springboot架构的。

RUN yum install kde-l10n-Chinese -y
RUN yum install glibc-common -y
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
RUN export LANG=zh_CN.UTF-8
RUN echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf
ENV LANG zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8

最终使用到的Dockerfile如下

  • 基础镜像

    FROM centos:7.9.2009
    
    # 基础配置,包括中文环境、时区等
    RUN yum install kde-l10n-Chinese -y
    RUN yum install glibc-common -y
    RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
    RUN export LANG=zh_CN.UTF-8
    RUN echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf
    ENV LANG zh_CN.UTF-8
    ENV LC_ALL zh_CN.UTF-8
    RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
    
    # 修改镜像源
    RUN rm -rvf /etc/yum.repos.d/*
    ADD https://d.op123.ren/d/soft/centos/CentOS-Base.repo /etc/yum.repos.d/
    ADD https://d.op123.ren/d/soft/centos/epel.repo /etc/yum.repos.d/
    
    # 安装基础软件
    RUN yum install -y vi openssh-clients tar gzip curl wget telnet rsync iftop iproute dstat sysstat lrzsz net-tools traceroute tcpdump tshark bind-utils
    ADD https://d.op123.ren/d/soft/trzsz/centos/trzsz_with_rzsz /bin/trzsz
    ADD https://d.op123.ren/d/soft/trzsz/centos/trz_with_rzsz /bin/trz
    ADD https://d.op123.ren/d/soft/trzsz/centos/tssh_with_rzsz /bin/tssh
    ADD https://d.op123.ren/d/soft/trzsz/centos/tsz_with_rzsz /bin/tsz
    RUN chmod a+x /bin/*
    
    # 清理缓存文件
    RUN yum clean all ; rm -rvf /var/cache/yum/*
    
    # 启动命令
    CMD ["/bin/bash"]
  • java镜像
# 依托镜像
FROM harbor.test.stesh.cn/op123/centos7.9.2009:cn20231208

# 安装jdk
RUN yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
ADD https://d.op123.ren/d/soft/centos/CentOS-Debuginfo.repo /etc/yum.repos.d/
RUN yum --enablerepo=*debug* install -y java-1.8.0-openjdk-debuginfo
ENV JAVA_HOME /usr/lib/jvm/jre-1.8.0-openjdk
ENV PATH $JAVA_HOME/bin:$PATH

# 清理
RUN yum clean all ; rm -rvf /var/cache/yum/*
最后修改:2024 年 05 月 11 日
如果觉得我的文章对你有用,请随意赞赏