Reputation: 554
Here is my source code:
public static byte[] encrypt(byte[] Data) throws Exception {
Log.i("Debug", "initial data is" + java.util.Arrays.toString(Data));
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data);
Log.i("Debug", "encrypted data is" + java.util.Arrays.toString(encVal));
;
return Base64.encode(encVal,0);
}
When the data length of the byte[] Data is around 800 kb or so, i keep on receiving.
java.lang.OutOfMemoryError
Can you please help me how can i change my code to avoid this issue? I had to encode with base64 to avoid the error of incomplete block in decryption.
Upvotes: 2
Views: 981
Reputation: 1502825
Well one problem is the logging you're doing - by converting the data into a string, you're probably using up to 5 characters per byte, and each character is 2 bytes, so that's 10 bytes in memory use per original byte. You're then using string concatenation, so that's doubling the amount of memory required (due to the copying).
So just the logging statements are each taking about 16MB I suspect. Is it really useful to you to have a full 800KB array logged?
What happens if you remove the log statements?
Upvotes: 2