I am using javax.crypto
package to encypt/decrypt files but the problem is that once a big file (around 100- 700 mb) is encrypted there is spike in memory of 70 Mb (first time) and whole of this memory is not released after execution is finished. I have kept my application run for days but this memory do not come down.
Interesting thing is if I encrpyt/ decrypt the same file again and again the memory do not rise by 70 Mb, but for first 3-4 iterations 5-8 Mb of memory is released in each iteration and after that memory starts increasing again in chunk of 2-5 Mb and after few iteration some memory get released but in all the memory always increases.
The code to encrypt file is simple
static Cipher c;// = Cipher.getInstance("AES/CBC/PKCS5Padding");
private static void Encrypt_File(String infile, String outFile) throws Exception
{
//String destKey = "123456";
//byte[] IV = generateRandomBytes(16);
//byte[] salt = generateRandomBytes(16);
//Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("123456", salt, 1000);
//SecretKey key = new SecretKeySpec(rfc.getBytes(32), "AES");
SecretKey key = new SecretKeySpec(generateRandomBytes(32), "AES");
//if(c == null)
c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key);
FileOutputStream fos = new FileOutputStream(outFile);
CipherOutputStream cos = new CipherOutputStream(fos, c);
FileInputStream fis = new FileInputStream(infile);
try
{
int len = 0;
byte[] buf = new byte[1024*128];
while((len = fis.read(buf)) != -1) {
cos.write(buf, 0, len);
//cos.flush();
}
}
finally
{
c.doFinal();
cos.flush();
cos.close();
fos.flush();
fos.close();
fis.close();
}
}
This is simple observation I have seen in my program: I am using Windows 7 64 bit with 16 GB RAM Intel Core 2 Duo 3.00 GHz and file encrypted was 700 MB size.
Explanation Memory Usage(Shown in Windows Task Manager Private Working Set column)
When program starts 9924 K
After first iteration of encryption 81,180 K
Second Iteration 78,254 K
3 Iteration 74,614 K
4 Iteration 69,523 K
5 Iteration 72,256 K
6 Iteration 70,152 K
7 Iteration 83,327 K
8 Iteration 85,613 K
9 Iteration 95,124 K
10 Iteration 92,698 K
11 Iteration 94,670 K
I kept the iteration on for 2000 iteration, the same pattern was observed and at the end memory usage 184,951 K, this memory was not released after calling System.gc()
also.
What could be the possible problem, is it the CipherOutputStream
or Cipher
class having some memory leak or I am doing something wrong here?
EDIT After seeing the link (posted in comment), I made changes so that I can print what the memory usage was in JVM i.e. added these lines in code
System.out.println(" " +i +" \t\t\t " + ConvertTOMB(Runtime.getRuntime().totalMemory()) +" \t\t "+ ConvertTOMB(Runtime.getRuntime().freeMemory()) +" \t\t "+ ConvertTOMB( Runtime.getRuntime().totalMemory() -Runtime.getRuntime().freeMemory()) );
Observed after 1000 iterations the memory usage increased and then did not returned to normal, as in first few iteration the memory usage was 1-5 MB but after 1000 iteration the memory consumption never returned to single digit, it was ranging 25-225 M.B.