Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory limitations #12

Open
A-KL opened this issue Jan 30, 2018 · 0 comments
Open

Memory limitations #12

A-KL opened this issue Jan 30, 2018 · 0 comments

Comments

@A-KL
Copy link

A-KL commented Jan 30, 2018

Hi,
I'm working on a simple app that communicates (sends POST requests) to a remote server in order to apply complex image effects. At the moment, APIs require an image to be base64 encoded and sent as a part of a JSON payload.

Everything works perfectly in emulator but on a real device - I get OutOfMemoryError. The code is pretty simple and seems to consume around 20mb (I did a few tests using the same emulator).

File image = new File("/mnt/sdcard/DCIM/" + info.getFolder() + "/" + info.getFilename());
FileInputStream inputStream = new FileInputStream(image);
int size = inputStream.available();
byte[] buffer = new byte[size]; // 4MB
inputStream.read(buffer);
inputStream.close();

String base64Data = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT); // 15MB

Stack trace from a device (Sony A6000) :

[ERROR] java.lang.RuntimeException: An error occured while executing doInBackground()
java.lang.RuntimeException: An error occured while executing doInBackground()
	at android.os.AsyncTask$3.done(AsyncTask.java:200)
	at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
	at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
	at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.OutOfMemoryError
	at android.util.Base64.encode(Base64.java:534)
	at android.util.Base64.encodeToString(Base64.java:477)
	at com.github.ma1co.pmcademo.app.ImageAsyncTask.doInBackground(ImageAsyncTask.java:42)
	at com.github.ma1co.pmcademo.app.ImageAsyncTask.doInBackground(ImageAsyncTask.java:13)
	at android.os.AsyncTask$2.call(AsyncTask.java:185)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
	... 4 more

The system itself seems to have around 82Mb of RAM available, so what could cause the issue? I'm pretty new to a mobile developent, but does Android has a limitation per activity\background task?

I'm using Android 4.04 for the emulator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant