0

In my app I was using a .txt file to save geopoints and strings, so instead of going through the process of deleting a bunch of newlines from a .txt I could just delete a row. So I put this in test for just saving data, saves fine, but when I load the data I get an out of memory erro for a bitmap that is 96x96 decoding a file, it just doesn't make sense to me.

This is the logcat:

03-28 09:48:30.059: E/dalvikvm-heap(4607): Out of memory on a 46096-byte  allocation.
03-28 09:48:30.059: I/dalvikvm(4607): "main" prio=5 tid=1 RUNNABLE
03-28 09:48:30.059: I/dalvikvm(4607):   | group="main" sCount=0 dsCount=0   obj=0x40a719a0 self=0x2a00bba8
03-28 09:48:30.059: I/dalvikvm(4607):   | sysTid=4607 nice=0 sched=0/0 cgrp=apps    handle=1073849308
03-28 09:48:30.070: I/dalvikvm(4607):   | state=R schedstat=( 9701142608 17839341793 3050 ) utm=828 stm=142 core=0
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328)
03-28 09:48:30.070: I/dalvikvm(4607):   at com.apps.gonefishing.Information.onCreate(Information.java:80)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.Activity.performCreate(Activity.java:5104)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-28 09:48:30.081: I/dalvikvm(4607):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-28 09:48:30.081: I/dalvikvm(4607):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-28 09:48:30.090: I/dalvikvm(4607):   at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 09:48:30.090: I/dalvikvm(4607):   at android.os.Looper.loop(Looper.java:137)
03-28 09:48:30.090: I/dalvikvm(4607):   at android.app.ActivityThread.main(ActivityThread.java:5039)
03-28 09:48:30.090: I/dalvikvm(4607):   at java.lang.reflect.Method.invokeNative(Native Method)
03-28 09:48:30.090: I/dalvikvm(4607):   at   java.lang.reflect.Method.invoke(Method.java:511)
03-28 09:48:30.090: I/dalvikvm(4607):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-28 09:48:30.090: I/dalvikvm(4607):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-28 09:48:30.090: I/dalvikvm(4607):   at dalvik.system.NativeStart.main(Native Method)
03-28 09:48:30.110: D/skia(4607): --- decoder->decode returned false
03-28 09:48:30.110: D/AndroidRuntime(4607): Shutting down VM
03-28 09:48:30.110: W/dalvikvm(4607): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-28 09:48:30.140: E/AndroidRuntime(4607): FATAL EXCEPTION: main
03-28 09:48:30.140: E/AndroidRuntime(4607): java.lang.OutOfMemoryError
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at com.apps.gonefishing.Information.onCreate(Information.java:80)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.Activity.performCreate(Activity.java:5104)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.os.Looper.loop(Looper.java:137)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at java.lang.reflect.Method.invoke(Method.java:511)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at dalvik.system.NativeStart.main(Native Method)

One thing I noticed is that every time the heap message comes up, there is only 5 to 6 percent available, so I started the emulator wiping the data and it's the same thing. This is an api 17 emulator, when the app was run with the text file it ran perfectly.

The next thing I'm going to try is; even when I open the database to read I'm using:

db = dbHelper.getWritableDatabase();

So I'm going to change it to getReadableDatabase

It's not that much of a change and in the activity that is before this one with the error, it finishes and everything. There are 2 bitmaps sent back and forth to eachother one on a canvas in a TextWatcher. I think it just takes memory of 2 bitmaps 320X480 ran fine with the .txt file.

Any advice is greatly appreciated, thank you for your time.

 Cursor cursor;
        db.read();
        cursor = db.getAllEntries();
        boolean go = cursor.moveToFirst();
        while(cursor.isAfterLast() != true){
            Drawable imagebig = null;
            Bitmap photobig = null;
            int lat = cursor.getInt(MyDBAdapter.LATITUDE_COLUMN);
            int lng = cursor.getInt(MyDBAdapter.LONGITUDE_COLUMN);
            GeoPoint recall = new GeoPoint(lat,lng);
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Gone");
            file = new File(file.getPath(),"Thumbs");
            String string = cursor.getString(MyDBAdapter.FILE_COLUMN);
            File file1 = new File(file.getPath() + File.separator + "thumb" + string);

            Uri uri = Uri.fromFile(file1);
            if(file1.exists()){
             //photobig = BitmapFactory.decodeFile(file1.getAbsolutePath());
             uri = Uri.fromFile(file1);

             try {
                photobig = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
        db.close();
        cursor.close();
JRowan
  • 6,824
  • 8
  • 40
  • 59
  • 1
    Doubt this has anything to do with your database. – Stefan de Bruijn Mar 28 '13 at 10:05
  • do you have some idea? im kinda clueless here the app ran perfectly and only switching the .txt file to sqlite made it start crashing – JRowan Mar 28 '13 at 10:08
  • im closing my gps listener i have no static bitmaps – JRowan Mar 28 '13 at 10:09
  • Well in theory i guess you could come short of that little bit of memory since the database takes a few kb but i doubt thats the case. Sounds more like some coincidence. – Stefan de Bruijn Mar 28 '13 at 10:09
  • I don't think this has anything to do with SQLite, instead, it looks to me you are doing some heavy processing with Bitmaps/images? What are you doing in `Information.java` line **80** ? – kaderud Mar 28 '13 at 10:11
  • BitmapFactory.decodeFile(file1.getAbsolutePath()); its a png 96X96 – JRowan Mar 28 '13 at 10:13
  • in my textwatcher i keep reloading the same bitmap to another bitmap 320X480 and writing different text onto it in a canvas but thats still one bitmap isnt it? – JRowan Mar 28 '13 at 10:16
  • in a different class that finishes right before information – JRowan Mar 28 '13 at 10:16
  • im like kind of in fear that hackers are in my computer because of this little change .txt to database or something – JRowan Mar 28 '13 at 10:20
  • heres more since the crash on the 96X96 loading bitmap, remember i only switched from txt to sqlite i optimised the app and got rid of 4 static bitmaps and alot of static variables methods and such and it still crashes on that same little bitmap, just a change from txt to sqlite, is it safe to suspect that it isnt even the app? my platform is just messed up – JRowan Mar 28 '13 at 12:53
  • i took out the textwatcher, still same thing – JRowan Apr 01 '13 at 03:27
  • You may have a memory leak. Can you post the code that handles the bitmap? Also, what Android version are you running? – Luis Apr 01 '13 at 03:27
  • api17 i im using a minimum 8 and target 17 same thing all across, i will post code i 1 sec – JRowan Apr 01 '13 at 03:31
  • ok, i posted the code for database and .txt im either using 1 or the other, for .txt it works fine, not for db. – JRowan Apr 01 '13 at 03:35
  • Older versions of Android (pre-Honeycomb) need to call recycle() on the bitmap. See http://stackoverflow.com/a/8996688/1172181 – Luis Apr 01 '13 at 03:36
  • im not fully sure what this means, after i load the bitmap call recycle and set it to null? – JRowan Apr 01 '13 at 03:41
  • One thing I noticed right away is that you have an infinite while loop since you don't advance the cursor. Your while loop needs cursor.moveToNext(); That will definitely cause an Out of Memory Error. – Luis Apr 01 '13 at 03:42
  • hey you got it thanks man, that was exactly the problem, put as answer you got it – JRowan Apr 01 '13 at 03:45
  • No problem. I've done the same thing many times... – Luis Apr 01 '13 at 03:46
  • ok i got you in 23 hours this is my first one, thanks alot man – JRowan Apr 01 '13 at 03:49

2 Answers2

3

You have an infinite while loop. The cursor needs to be advanced.

Add cursor.moveToNext(); to your while loop.

Luis
  • 3,451
  • 1
  • 27
  • 41
0

And change this

while(cursor.isAfterLast() != true){

for that

while(!cursor.isAfterLast()){

it hurts. *Don't take it in a bad way :)

Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26