Forum Stats

  • 3,838,749 Users
  • 2,262,397 Discussions
  • 7,900,750 Comments

Discussions

Memory does not get released after encrypting/ decrypting files.

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

<span style="font-size: 10pt; font-family: verdana, geneva;">Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");</span>
byte[] salt = generateRandomBytes(16);
Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("123456", salt, 1000);
SecretKey key = new SecretKeySpec(rfc.getBytes(32), "AES");
<br/><span style="font-family: verdana, geneva; font-size: 10pt;">c.init(Cipher.ENCRYPT_MODE, <span style="font-family: verdana, geneva;">key </span>);</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">FileOutputStream fos = new FileOutputStream(encryptedFile);</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">CipherOutputStream cos = new CipherOutputStream(fos);</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">FileInputStream fis = new FileInputStream(largeInputFile);</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">int len = 0;</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">byte[] buf = new byte[1024 * 128];</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">while((len = fis.read(buf)) != -1) {</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">   cos.write(buf, 0, len);</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">}</span><br/><span style="font-family: verdana, geneva; font-size: 10pt;">cos.close();</span><br/><span style="font-size: 10pt; font-family: verdana, geneva;">fis.close();</span>

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?

Tagged:
This discussion has been closed.