微信小程序獲取用戶解密的Session_key 而後對 encryptedData進行解密 偶爾報錯 時間長了以後會報內存溢出:java
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);
}
改爲以下方式ide
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不變,而後第三次能夠成功。blog
實時分析java佔用cpu的進程及線程,找到線程對應的java代碼。進程
top -Hp pid
jstack pid下的線程pid
參考了微信中的文章
https://mp.weixin.qq.com/s/ZqlhPC06_KW6a9OSgEuIVw
可是貌似並非全部的問題代碼都能在裏面定位到。