项目中用到的代码如下:
Cipher cipher = Cipher.getInstance("RSA", new BouncyCastleProvider());
项目长时间运行,就会出现JceSecurity占用的内存越来越多,而且不会释放。
具体分析请看这篇https://timerbin.iteye.com/blog/2151969
有如下修复的方法:
方法一、
private static BouncyCastleProvider bouncyCastleProvider = null;
public static synchronized BouncyCastleProvider getInstance() {
if (bouncyCastleProvider == null) {
bouncyCastleProvider = new BouncyCastleProvider();
}
return bouncyCastleProvider;
}
......
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", getInstance());
方法二、
Provider provider=Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
if(provider==null){
provider=new BouncyCastleProvider();
Security.addProvider(provider);
}
Cipher cipher = Cipher.getInstance("RSA","BC");
使用BouncyCastleProvider加密的时候千万别乱new BouncyCastleProvider()
切记!!!
切记!!!
切记!!!

在使用BouncyCastleProvider进行RSA加密时,发现JceSecurity内存占用持续上升且不释放。本文提供两种解决方法,避免频繁new BouncyCastleProvider导致的内存泄漏。

被折叠的 条评论
为什么被折叠?



