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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

843793
I can't remember exactly how it works, but I'm pretty sure you won't be allowed to compile code that breaks genericity.

Remember, a generic class provides genericity, but you provide the parameter. The only situation that breaks this is if member variables are public - which they shouldn't be.
843793
Hi,
According to the GJ specification, the compiler does store extra parameter information in the class files. There is a process called 'retrofitting' which allows to add this extra information to existing classes.
The Java 2 Collection classes which come with the generics compiler were retrofitted in this way. If, however, you use the 'raw types' it is possible to compile code which violates type constraints, and which will fail only at runtime (the compiler will give an unchecked warning, though).
843793
No its not a limitation but the nature of late binding. Sure you can use a precompiled class like it were a library and access the methods directly since you will know what the signatures are. You are within your rights to create as many illegal casts as you want. The point is Generics will not itself create any bad casts. Nobody can stop a mad programmer...
1 - 3
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,227 views