0

I'm relatively new to Android and have been developing a simple app for a little while. All the features of the app have been working however now I have encountered a java.lang.OutOfMemoryError: [memory exhausted] error.

I have read up online about how this error is caused but most of these answers are about bitmap images using up too much memory. My app only uses image buttons that are 50dp x 50dp big so I don't think it's these are causing the error.

I have used the DDMS and looked into the heaps etc. The picture below illustrates that a high amount of the memory is being used however the data on how means nothing to me.

Screenshot of DDMS log

To give you an outline my app is basically a series of input forms and the data is stored in the database. One of the input forms is 50 fields long so maybe the problem is here somewhere? I am using the Genymotion emulator to run/test my application.

I would appreciate it if somebody could highlight where the memory is being used up and how to solve this problem.

Thanks in advance.

EDIT: The code below posted by Kristy helped to identify where the error was, from there I was able to spot the issue which was very simple cursor.moveToNext was actually cursor.moveToFirst this obviously caused an issue with the cursor which caused the program to run out of memory.

jwd40
  • 45
  • 1
  • 6

1 Answers1

0

If you're obtaining the bitmaps from file rather than them being packed into the apk, you can run out of memory. Another thing to look at is to make sure you've closed the cursor doing database calls. This helped me to put it right after the super.OnCreate() method:

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects()
            .penaltyLog()
            .penaltyDeath()
            .build());
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • Thank you for posting, I have gone through all my database query routines and ensured that all cursors have been closed, but still the error remains. Do I paste this code into the OnCreate method of the Database handler class or the class where it was called? – jwd40 Dec 22 '15 at 13:28
  • Class where it is called, in the onCreate() – Kristy Welsh Dec 22 '15 at 13:56