{"id":2329,"date":"2022-08-30T15:23:57","date_gmt":"2022-08-30T15:23:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/android-application-crashes-after-starting-it-second-or-third-time-showing-memory-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:57","modified_gmt":"2022-08-30T15:23:57","slug":"android-application-crashes-after-starting-it-second-or-third-time-showing-memory-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-application-crashes-after-starting-it-second-or-third-time-showing-memory-error-collection-of-common-programming-errors\/","title":{"rendered":"Android application crashes after starting it second or third time (Showing memory error)-Collection of common programming errors"},"content":{"rendered":"<p>I am working on application which uses downloaded images from my application to display in the background. When I start application it works fine and load image without any problem. But after several restart application crashes with memory error. I use below method for get Drawable to show it in View. I have used one class and which having this static method I use it for all images. I also call one other static method for clear bitmap but don&#8217;t know it works or not. May be I do not able to clear memory when I close application.<\/p>\n<pre><code>    public static void recycle_bitmap() {\n    if (myBitmap != null) {\n        myBitmap.recycle();\n        myBitmap = null;\n\n    }\n\n    }\n\n    static Bitmap myBitmap = null;\n\n    public static Drawable ImgDrawableFromFile(Resources res, String file_name) {\n\n    myBitmap=null;\n    File imgFile = new File(\"\/data\/data\/com.appstart\/app_my_sub_dir\/\"\n            + file_name + \".jpg\");\n    if (imgFile.exists()) {\n\n        myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());\n        if (myBitmap != null)\n            return new BitmapDrawable(res, myBitmap);\n        else\n            return null;\n    }\n    return null;\n\n    }\n<\/code><\/pre>\n<p>Logcat error<\/p>\n<pre><code>01-11 12:09:41.860: D\/dalvikvm(6047): GC_EXTERNAL_ALLOC freed 598K, 48% free                  3336K\/6407K, external 8683K\/10523K, paused 274ms\n01-11 12:09:42.149: E\/dalvikvm-heap(6047): 1671840-byte external allocation too large for this process.\n01-11 12:09:42.480: I\/dalvikvm-heap(6047): Clamp target GC heap from 16.094MB to 16.000MB\n01-11 12:09:42.480: E\/GraphicsJNI(6047): VM won't let us allocate 1671840 bytes\n01-11 12:09:42.501: D\/dalvikvm(6047): GC_FOR_MALLOC freed 1K, 48% free 3334K\/6407K,    external 8674K\/10523K, paused 247ms\n01-11 12:09:42.501: D\/skia(6047): --- decoder-&gt;decode returned false\n01-11 12:09:42.501: W\/dalvikvm(6047): threadid=15: thread exiting with uncaught exception (group=0x40015560)\n01-11 12:09:42.560: E\/AndroidRuntime(6047): FATAL EXCEPTION: Thread-18\n01-11 12:09:42.560: E\/AndroidRuntime(6047): java.lang.OutOfMemoryError: bitmap size exceeds VM budget\n01-11 12:09:42.560: E\/AndroidRuntime(6047):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)\n01-11 12:09:42.560: E\/AndroidRuntime(6047):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)\n01-11 12:09:42.560: E\/AndroidRuntime(6047):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:284)\n01-11 12:09:42.560: E\/AndroidRuntime(6047):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:309)\n01-11 12:09:42.560: E\/AndroidRuntime(6047):     at com.appstart.utility.LoadImage.ImgDrawableFromFile(LoadImage.java:30)\n01-11 12:09:42.560: E\/AndroidRuntime(6047):     at com.appstart.MainActivity.run(MainActivity.java:70)\n01-11 12:09:42.560: E\/AndroidRuntime(6047):     at java.lang.Thread.run(Thread.java:1019)\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-01-05 10:10:47. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am working on application which uses downloaded images from my application to display in the background. When I start application it works fine and load image without any problem. But after several restart application crashes with memory error. I use below method for get Drawable to show it in View. I have used one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2329","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2329"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2329\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}