Memory does not get released after encrypting/ decrypting files.

10-11  Source: Network gathering  Views:0 

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 = != -1) {
   cos.write(buf, 0, len);
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
Memory Usage (As 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?
ash wrote:
We are using WebLogic Server 7.0 runing on Solaris 2.7.
We are experiencing a problem where the memory does not seem to be released after
the application has been shut down.What do you mean by "application has been shut down"? Is the server
process running or not? Is it a zombie?
Unix 'top' command reports that memory has not been reclaimed by the O/S. What exactly has it reported? If the process is gone, then I'm pretty
sure your O/S has reclaimed the memory. What exactly are you looking at
in top?
-- Rob
> Continue
restarting it will forces us to reboot the server as there will be more and more
memory lost when restarting the WebLogic.
Advice to fix the problem is much appreciated. Thanks.
Related articles