天天看点

每次New一个BouncyCastleProvider导致的内存泄漏

问题原因

生产服务器的内存一直升高,直到宕机。内存分析的结论是:内存泄漏。

下载dump文件,经过解析堆栈日志,如下图所示:

每次New一个BouncyCastleProvider导致的内存泄漏
The class "javax.crypto.JceSecurity", loaded by "<system class loader>", occupies ,,, (%) bytes. The memory is accumulated in one instance of "java.lang.Object[]" loaded by "<system class loader>".Keywords
java.lang.Object[]
javax.crypto.JceSecurity
           
每次New一个BouncyCastleProvider导致的内存泄漏

是BouncyCastleProvider 这个类里面的方法, 全是静态的, new一个多一个, GC不回收, 慢慢就宕机。

dump代码如下:

每次New一个BouncyCastleProvider导致的内存泄漏

解决方案

private static org.bouncycastle.jce.provider.BouncyCastleProvider bouncyCastleProvider = null;

    public static synchronized org.bouncycastle.jce.provider.BouncyCastleProvider getInstance() {
        if (bouncyCastleProvider == null) {
            bouncyCastleProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
        }
        return bouncyCastleProvider;
    }

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", getInstance());
           

内存泄漏分析工具参见:http://blog.csdn.net/shenhaiwen/article/details/54670234