0

I have an app that deals with images, it works fine with some devices ex. HTC ONE X, and others dont ex. GALAXY NOTE, the list is big IDK the problem exatcly cuz it works in some, and it dont in others. also it doesnt work on the emulator the default one but a small one it does work

this is the logcat I get

08-15 18:30:39.070: I/dalvikvm-heap(334): Clamp target GC heap from 25.723MB to 24.000MB
08-15 18:30:39.090: D/dalvikvm(334): GC_FOR_MALLOC freed <1K, 51% free 2672K/5379K, external 19015K/19657K, paused 38ms
08-15 18:30:39.370: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed <1K, 51% free 2672K/5379K, external 19015K/19657K, paused 45ms
08-15 18:30:39.400: E/dalvikvm-heap(334): 810000-byte external allocation too large for this process.
08-15 18:30:39.510: I/dalvikvm-heap(334): Clamp target GC heap from 25.724MB to 24.000MB
08-15 18:30:39.510: E/GraphicsJNI(334): VM won't let us allocate 810000 bytes
08-15 18:30:39.510: D/dalvikvm(334): GC_FOR_MALLOC freed 0K, 51% free 2672K/5379K, external 19015K/19657K, paused 36ms
08-15 18:30:39.532: D/AndroidRuntime(334): Shutting down VM
08-15 18:30:39.532: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-15 18:30:39.600: E/AndroidRuntime(334): FATAL EXCEPTION: main
08-15 18:30:39.600: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.com.android.editor/app.com.android.editor.Option}: android.view.InflateException: Binary XML file line #317: Error inflating class <unknown>
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-15 18:30:39.600: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334): Caused by: android.view.InflateException: Binary XML file line #317: Error inflating class <unknown>
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.Activity.setContentView(Activity.java:1657)
08-15 18:30:39.600: E/AndroidRuntime(334):  at app.com.android.editor.Option.onCreate(Option.java:40)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-15 18:30:39.600: E/AndroidRuntime(334):  ... 11 more
08-15 18:30:39.600: E/AndroidRuntime(334): Caused by: java.lang.reflect.InvocationTargetException
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Constructor.constructNative(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
08-15 18:30:39.600: E/AndroidRuntime(334):  ... 25 more
08-15 18:30:39.600: E/AndroidRuntime(334): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.nativeCreate(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.View.<init>(View.java:1951)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.widget.TextView.<init>(TextView.java:344)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.widget.Button.<init>(Button.java:108)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.widget.Button.<init>(Button.java:104)
08-15 18:30:39.600: E/AndroidRuntime(334):  ... 28 more
08-15 18:30:45.459: I/Process(334): Sending signal. PID: 334 SIG: 9
John Jared
  • 790
  • 3
  • 16
  • 40

4 Answers4

4

Heap sizes vary by device. It is perfectly reasonable that on devices where apps have large heap sizes you will encounter fewer problems than on devices where apps have smaller heap sizes. Your heap could be as low as 16MB, if you are supporting older/lower-resolution devices.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm supporting that, and I'm using a decoder by Fedor. why it's still there ? anything more should I user ? – John Jared Aug 15 '12 at 16:41
  • @AhmedAl-ekrii: "I'm using a decoder by Fedor" -- I am sure that Fedor appreciates this, but I have no idea what you are talking about. "why it's still there ?" -- if "it's" is your `OutOfMemoryError`, it is because you do not have enough heap space to allocate this `Bitmap`. That could be because you are out of space in general, or that there is not a big enough contiguous block of memory (e.g., you want 800KB, you have 2MB of heap available, but there is no single 800+KB block). – CommonsWare Aug 15 '12 at 16:46
  • Thanks, and I get more heap space by recycling right ? and do you recommend weak reference ? cuz some does, and some doesn't – John Jared Aug 15 '12 at 16:50
  • @AhmedAl-ekrii: Calling `recycle()` on your `Bitmap` objects when you are done with them will free up their memory more quickly than will just letting them be garbage-collected. `SoftReference` and `WeakReference` are not recommended, as seen here in the documentation: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html – CommonsWare Aug 15 '12 at 17:09
2

Most android has limited space. Usually 16MB. I highly suggest looking at your image size. Also use weak references where you can to make sure images are only using memory when accessed. (Weak references will force the garbage collected to clean them up quicker.)

  • And I don't know much about your app with just your logcat error list, but if you have multiple activities, kill the ones you don't need. Might seem like obvious advice, maybe not, but it will free up space.
jos
  • 431
  • 2
  • 9
  • Great suggestion. Here's more on ["weak references"](http://stackoverflow.com/questions/3243215/how-to-use-weakreference-in-java-and-android-development#). BOTTOM LINE: if your handset doesn't have enough RAM to allocate another 800K, then it simply doesn't have enough memory. It *will* fail sooner or later *regardless*. You just need to load fewer, smaller images :) – paulsm4 Aug 15 '12 at 15:46
  • thanks for the vote! I actually had this same issue two weeks back and when I used weak references it helped my program immensely! – jos Aug 15 '12 at 16:14
  • Only thing I'd avoid (I read a lot of opinions on this issue when I had it. They're a mixed bag.) is to directly call the garbage collector via something like System.gc(). "Generally speaking, in the presence of a garbage collector, it is never good practice to manually call the GC. A GC is organized around heuristic algorithms which work best when left to their own devices. Calling the GC manually often decreases performance." (Thanks to Thomas Pornin on stackoverflow for a nicely worded answer. I always reference his response to gc useage because it's worded so well.) – jos Aug 15 '12 at 19:43
  • almost forgot! If you want to check what's using up space and how much, install the eclipse memory analyzer tool (MAT) for android apps. There's really straightforward instructions online on how to set it up and use it. Extremely useful when programming for android! – jos Aug 15 '12 at 19:51
2

As others stated, the heap size for an Application is limited. The minimum is 16MB and veries from device to device (it's a setting of the virtual machine).

Some suggestions what you could do:

  1. You can load a smaller version of the image, if not needed in full resolution. Here is how to do it: http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
  2. Check if enough free memory is available; If memory gets low, you can just free images that are not directly needed right now and load them again, if they are needed.

Here is some code sniped for checking how much free memory is left:

long free = Runtime.getRuntime().freeMemory();
SDwarfs
  • 3,189
  • 5
  • 31
  • 53
1
 Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

that line says it all. The bitmaps are hogging all the memory of some devices (devices that the VM stack limit is higher). You should look into better memory management for this application such as LruCache, decode bitmaps with lower quality, etc.

edit: do not use WeakReference as some users might suggest. check this question Does Android need to load a complete Bitmap from a file before sampling it down? and also this video http://www.youtube.com/watch?v=gbQb1PVjfqM

direct explanations/teachings/techniques from the creators of the platform and say to not use WeakReference.

Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144