记一次程序内存占用高问题排查

x33g5p2x  于2022-06-10 转载在 其他  
字(4.4k)|赞(0)|评价(0)|浏览(365)

登录机器,查看内存占用

使用PID将内存占用DMUP下来

jmap -dump:format=b,file=heap.bin 23711

文件竟然6.2G

分析dump文件

分析结论:内存中存储的数据过大,需要清理

查看系统GC情况

jstat -gc 23711 1000

S0C:年轻代中第一个survivor(幸存区)的容量 (字节)
S1C:年轻代中第二个survivor(幸存区)的容量 (字节)
S0U :年轻代中第一个survivor(幸存区)目前已使用空间 (字节)
S1U :年轻代中第二个survivor(幸存区)目前已使用空间 (字节)
EC :年轻代中Eden(伊甸园)的容量 (字节)
EU :年轻代中Eden(伊甸园)目前已使用空间 (字节)
OC :Old代的容量 (字节)
OU :Old代目前已使用空间 (字节)
MC:metaspace(元空间)的容量 (字节)
MU:metaspace(元空间)目前已使用空间 (字节)
YGC :从应用程序启动到采样时年轻代中gc次数
YGCT :从应用程序启动到采样时年轻代中gc所用时间(s)
FGC :从应用程序启动到采样时old代(全gc)gc次数
FGCT :从应用程序启动到采样时old代(全gc)gc所用时间(s)
GCT:从应用程序启动到采样时gc用的总时间(s)

查看本进程被分配的内存

jinfo pid

查看内存占用前10的类

jmap -histo:live 23711  | sort -n -r -k 2 | head -20

分析原因:

内存中存在大数据对象放在hashMap中,并没有即时回收,造成内存占用越来越大。

解决方案

hashMap设置过期时间,将内存数据回收

cacheObject.java

/**
 * @program: javademo
 * @description:
 * @author: sunyuhua
 * @create: 2022-06-06 10:20
 **/
public class CacheObject {

 /**
  * 缓存对象
  */
 private Object value;

 /**
  * 生存时间
  */
 private long ttl;

 /**
  * 构造函数
  *
  * @param value 缓存对象
  * @param ttl 生存时间毫秒
  */
 public CacheObject(Object value, long ttl) {
  this.value = value;
  this.ttl = ttl;
 }

 public Object getValue() {
  return value;
 }

 public void setValue(Object value) {
  this.value = value;
 }

 public long getTtl() {
  return ttl;
 }

 public void setTtl(long ttl) {
  this.ttl = ttl;
 }
}

ConcurrentMapCache.java

/**
 * @program: javademo
 * @description:
 * @author: sunyuhua
 * @create: 2022-06-06 10:19
 **/
import org.apache.commons.lang3.ObjectUtils;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ConcurrentMapCache {

 /**
  * 缓存对象
  */
 private ConcurrentMap<String, CacheObject> cache = new ConcurrentHashMap<String, CacheObject>();

 /**
  * 定时删除器
  */
 private Timer deleteTimer = null;

 /**
  * 默认删除时间(秒)
  */
 private static final int DEFAULT_DELETE_SECONDS = 5 * 60;

 public ConcurrentMapCache(int deleteSeconds) {
  this.initTimer(deleteSeconds);
 }

 public ConcurrentMapCache() {
  this.initTimer(0);
 }

 /**
  * 初始化定时器
  *
  * @param deleteSeconds
  */
 private void initTimer(int deleteSeconds) {

  if (deleteSeconds == 0) {
   deleteSeconds = DEFAULT_DELETE_SECONDS;
  }

  deleteTimer = new Timer();
  deleteTimer.schedule(new DeleteTask(), 0, deleteSeconds * 1000);
 }

 /**
  * 设置缓存
  *
  * @param key
  * @param value
  * @param expire 过期时间毫秒为单位
  */
 public void set(String key, Object value, long expire) {

  // ObjectUtils.isAnyEmpty 参数中只要存在empty即返回true
  if ( ObjectUtils.isEmpty(key) || ObjectUtils.isEmpty(expire) || ( expire < 0L && expire != -1 ) ) {
   return ;
  }

  CacheObject cacheObject = new CacheObject(value, expire == -1L ? expire : System.currentTimeMillis() + expire);

  this.cache.put(key, cacheObject);
 }

 /**
  * 设置过期时间
  * @param key
  * @param expire -1L长期有效
  */
 public void put(String key,long expire) {

  if ( ObjectUtils.isEmpty(key) || ObjectUtils.isEmpty(expire) || ( expire < 0L && expire != -1 ) ) {
   return ;
  }

  CacheObject cacheObject = this.cache.get(key);
  cacheObject.setTtl(expire == -1L ? expire : System.currentTimeMillis() + expire);
 }

 /**
  * 设置缓存
  *
  * @param key
  * @param value
  */
 public void set(String key, Object value) {

  this.set(key, value, -1L);

 }

 /**
  * 获取缓存
  */
 public Object get(String key) {

  if (this.check(key)) {
   return this.cache.get(key).getValue();
  }
  return null;
 }

 /**
  * 删除某个缓存
  */
 public void delete(String key) {

  this.cache.remove(key);

 }


 /**
  * 判断缓存是否有效(过期、不存在均返回false)
  */
 private boolean check(String key) {

  CacheObject cacheObject = this.cache.get(key);

  if (cacheObject == null) {
   return false;
  }

  if (cacheObject.getTtl() == -1L) {
   return true;
  }

  if (cacheObject.getTtl() < System.currentTimeMillis()) {
   delete(key);
   return false;
  }

  return true;
 }

 /**
  * 定时删除任务
  */
 class DeleteTask extends TimerTask {

  @Override
  public void run() {

   // 循环Map中数据,找到已经过期的数据进行删除
   for(Map.Entry<String, CacheObject> entry : cache.entrySet()) {

    if (entry.getValue().getTtl() < System.currentTimeMillis() && entry.getValue().getTtl() != -1L) {
     delete(entry.getKey());
    }
   }
	   this.cancel();
  }
 }
}

测试类

/**
 * @program: javademo
 * @description:
 * @author: sunyuhua
 * @create: 2022-06-06 10:46
 **/
public class ConTest {
 public static void main(String[] args) {
  ConcurrentMapCache concurrentMapCache=new ConcurrentMapCache();
  concurrentMapCache.set("score","12:13:09",10*1000);

  Timer timer = new Timer();
  timer.schedule(new TimerTask() {
   @Override
   public void run() {
    System.out.println(concurrentMapCache.get("score"));
   }
  }, 0L, 1000L);
 }
}

修改JVM参数配置:

JAVA_OPTS='-server -Xmn1g -Xms9g -Xmx9g -Xss512K -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/timerbackend_gc.hprof -XX:+ParallelRefProcEnabled  -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1g'
辅助:
1、将项目中不必要的加载内存的地方去掉,减少内存占用

2、将通知类拿走,直接发送到MQ,MQ再做消费或者转发

相关文章