1. 程式人生 > >微信小程序用戶信息解密失敗導致的內存泄漏問題。

微信小程序用戶信息解密失敗導致的內存泄漏問題。

exception top con invalid head CP HA memory tin

微信小程序獲取用戶解密的Session_key 然後對 encryptedData進行解密 偶爾報錯 時間長了之後會報內存溢出:

java.lang.OutOfMemoryError: GC overhead limit exceeded

at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:426)
at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:322)
at javax.crypto.JarVerifier.verify(JarVerifier.java:250)
at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:160)
at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:186)
at javax.crypto.Cipher.getInstance(Cipher.java:653)

看代碼是java解密的時候報錯了,

網上找了一篇文章,https://www.zhihu.com/question/40492755

然後將老代碼



public byte[] aesCbcDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
//密鑰
SecretKey k = new SecretKeySpec(keyBytes, "AES");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding",
 new BouncyCastleProvider()
);

cipher.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));
//執行操作
return cipher.doFinal(content);
}

改成如下方式

private static Provider provider = new BouncyCastleProvider();

public byte[] aesCbcDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
//密鑰
SecretKey k = new SecretKeySpec(keyBytes, "AES");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", provider);

cipher.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));
//執行操作
return cipher.doFinal(content);
}

用Jmeter 100並發壓測 可以檢測到老代碼內存在上升然後短時間不會釋放,新代碼上升然後穩定(和知乎文章鏈接中的結果一致),而且新代碼運行速度也更快(不用每次都new了)。

老代碼cpu:

技術分享圖片

新代碼cpu:

技術分享圖片

微信服務器返回的數據為什麽解密失敗,這個原因還是待查。同一個用戶,前兩次解密失敗,Session_key不變,然後第三次可以成功。

實時分析java占用cpu的進程及線程,找到線程對應的java代碼。

top -Hp pid

jstack pid下的線程pid

參考了微信中的文章

https://mp.weixin.qq.com/s/ZqlhPC06_KW6a9OSgEuIVw

但是貌似並不是所有的問題代碼都能在裏面定位到。








微信小程序用戶信息解密失敗導致的內存泄漏問題。