Hi everyone,
our app downloads several files locally to make them available offline.
It often happens that the app closes with this error in the console.
java.lang.OutOfMemoryError: Failed to allocate a 268435472 byte allocation with 25165824 free bytes and 96MB until OOM, max allowed footprint 460932552, growth limit 536870912
We have already tried to modify the manifest but we have not achieved great results.
Is there a way to configure infinite memory usage?
Do you have a solution to this problem?
So just checking, you’ve set
android:largeHeap="true"
In your manifest?
Depending on how you are downloading these files (native java or with JavaScript), you’ll have different solutions.
If you’re doing it in Java, you just need to follow best practices with how your write your code.
Can you provide some more information?
We have already tried with android: largeHeap = “true” without any success.
We download the files with a javascript function and the File plugin: https://ionicframework.com/docs/native/file
The customer’s request is to have several files available offline and for this reason we download many files via javascript at the first installation and save them on the filesystem.
After a certain amount of time the app closes and in the console I have the error reported above
I am generally a huge homer for Angular’s HttpClient
, but here’s one case where I would investigate using fetch
instead, because it specifically allows streaming. Anything that fundamentally relies on XHR (like HttpClient
does) won’t really be able to break giant objects down into manageable chunks, which I think could really help control your memory footprint here.
Usually, this error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
Therefore you pretty much have two options:
-
Increase the default memory your program is allowed to use using the -Xmx option (for instance for 1024 MB: -Xmx1024m)
-
Modify your program so that it needs less memory, using less big data structures and getting rid of objects that are not any more used at some point in your program
Increasing the heap size is a bad solution, 100% temporary. It will crash again in somewhere else. To avoid these issues, write high performance code.
- Use local variables wherever possible.
- Make sure you select the correct object (EX: Selection between String, StringBuffer and StringBuilder)
- Use a good code system for your program(EX: Using static variables VS non static variables)
- Other stuff which could work on your code.
- Try to move with Multy Threading