1、使用 top 命令查看cpu和内存使用情况
2、使用 top -Hp pid 命令查看pid进程内线程情况
3、使用 printf “%X\n” 线程id 命令 输出对应的十六进制码
4、使用 jstack pid|grep 线程id十六进制码 命令查询具体线程信息
5、使用 jstack pid >> 123.txt 命令 把Full thread dump全部存到文件中
查看保存的文件
搜索cpu内存占用高的线程。
查看对应的后台代码
跟踪代码发现
使用jconsole 分析
public class AuditLog implements Runnable
{
protected Log log = LogFactory.getLog(AuditLog.class);
public static final int DEFAULT_BATCH_SIZE = 1;
private static SafeauditLogService safeauditLogService = UapServicLocator.getService(SafeauditLogService.class,
"safeauditLogService");
private static List<SafeauditLog> allLog = new ArrayList<SafeauditLog>();
public AuditLog(SafeauditLog log)
{
allLog.add(log);
}
public void run()
{
// 此处有循环会导致cpu内存使用高
/**
while (true)
{
execute();
}*/
execute();
}
public void execute()
{
synchronized (allLog)
{
if (allLog.size() >= DEFAULT_BATCH_SIZE)
{
try
{
safeauditLogService.batchAdd(allLog);
} catch (Exception e)
{
e.printStackTrace();
if (log.isDebugEnabled())
{
log.debug("日志记录失败:", e);
}
} finally
{//即使添加失败也要删除,否者会一直循环添加失败的数据。
allLog.clear();
}
}
}
}
}
内存使用情况对比
有while循环代码
无while循环代码
线程阻塞情况对比
有while循环代码
无while循环代码