androidandroid-studioandroid-debugging

Out of Memory Error while Debugging on android device


This is the error which appear while debugging on my android device. But, while running my application on the Emulator, it returns everything completely correct.

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.OutOfMemoryError
                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
                  at android.content.res.Resources.loadDrawable(Resources.java:2854)
                  at android.content.res.Resources.getDrawable(Resources.java:1521)
                  at android.widget.ImageView.resolveUri(ImageView.java:628)
                  at android.widget.ImageView.setImageResource(ImageView.java:361)
                  at com.example.prof_mohamed.ksa.accmanagerAdapter.getView(accmanagerAdapter.java:72)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2570)
                  at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
                  at android.widget.ListView.onMeasure(ListView.java:1165)
                  at android.view.View.measure(View.java:16420)
                  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChild(ViewGroup.java:5025)
                  at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:393)
                  at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:419)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2549)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2188)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1333)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1544)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5089)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                  at android.view.Choreographer.doFrame(Choreographer.java:561)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
                  at android.os.Handler.handleCallback(Handler.java:725)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.A*ctivityThread.main(ActivityThread.java:5283)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                  at dalvik.system.NativeStart.main(Native Method)

can any body help to solve this problem. Thank You


Solution

  • To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory. Load a Scaled Down Version into Memory.As the image dimensions are known, they can be used to decide if the full image should be loaded into memory or if a subsampled version should be loaded instead. Here are some factors to consider:

    1. Estimated memory usage of loading the full image in memory.

    2. Amount of memory you are willing to commit to loading this image given any other memory requirements of your application.

    3. Dimensions of the target ImageView or UI component that the image is to be loaded into.

    4. Screen size and density of the current device.

    For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.

    For more details, read this