Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Memory does not get released after encrypting/ decrypting files.

1044746Oct 2 2013

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

Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte[] salt = generateRandomBytes(16);

Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("123456", salt, 1000);

SecretKey key = new SecretKeySpec(rfc.getBytes(32), "AES");


c.init(Cipher.ENCRYPT_MODE, key );
FileOutputStream fos = new FileOutputStream(encryptedFile);
CipherOutputStream cos = new CipherOutputStream(fos);
FileInputStream fis = new FileInputStream(largeInputFile);
int len = 0;
byte[] buf = new byte[1024 * 128];
while((len = fis.read(buf)) != -1) {
   cos.write(buf, 0, len);
}
cos.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

ExplanationMemory Usage (As shown in Windows Task Manager Private Working Set column)
When program starts9924 K
After first iteration of encryption81,180 K
Second Iteration78,254 K
3 Iteration74,614 K
4 Iteration69,523 K
5 Iteration72,256 K
6 Iteration70,152 K
7 Iteration83,327 K
8 Iteration85,613 K
9 Iteration95,124 K
10 Iteration92,698 K
11 Iteration94,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?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 30 2013
Added on Oct 2 2013
0 comments
1,238 views