1、使用 top 命令查看cpu和内存使用情况

image.png
2、使用 top -Hp pid 命令查看pid进程内线程情况

image.png
3、使用 printf “%X\n” 线程id 命令 输出对应的十六进制码

image.png
4、使用 jstack pid|grep 线程id十六进制码 命令查询具体线程信息

image.png
5、使用 jstack pid >> 123.txt 命令 把Full thread dump全部存到文件中

image.png
查看保存的文件

image.png
搜索cpu内存占用高的线程。

image.png
查看对应的后台代码

image.png
跟踪代码发现

image.png
使用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循环代码

image.png
无while循环代码

image.png
线程阻塞情况对比
有while循环代码

image.png
无while循环代码

image.png