天天看點

每次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