0

I am implementing SensorEventListner in my activities. I am having three activities where SensorEventListner is implementing to detect shake motion. In all three activities differently, I am registering the sensorListner onCreate and unregistering the listner onPause. On call of every activity it registers again and then `unregisters onPause.

My app having allocated heap size as 3.6MB while heap size of device is 16MB still getting Force close with the error in LogCat as:

" OutOfMemoryException: bitmap size exceeds VM budget" EDIT: Logcat error

06-13 15:47:35.056: ERROR/AndroidRuntime(10377): Uncaught handler: thread main exiting due to uncaught exception
06-13 15:47:35.196: ERROR/AndroidRuntime(10377): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:464)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:340)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.content.res.Resources.loadDrawable(Resources.java:1705)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.content.res.Resources.getDrawable(Resources.java:580)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.widget.ImageView.resolveUri(ImageView.java:548)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.widget.ImageView.setImageResource(ImageView.java:270)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at com.nga.flashcards.activities.GardenActivity.display(GardenActivity.java:1024)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at com.nga.flashcards.activities.GardenActivity.onSensorChanged(GardenActivity.java:1352)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:435)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.os.Looper.loop(Looper.java:123)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at android.app.ActivityThread.main(ActivityThread.java:4595)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at java.lang.reflect.Method.invokeNative(Native Method)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at java.lang.reflect.Method.invoke(Method.java:521)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-13 15:47:35.196: ERROR/AndroidRuntime(10377):     at dalvik.system.NativeStart.main(Native Method)

I am also having large amount images and changing images in imageview through setImageResource(). Getting error on using SensorEventListner. If sensor event is not used in app then working fine.

My questions are :

  • Wheather there are any issues related to memory while registering and unregistering listener again and again in the app?

  • Are there any memory leaks, if yes then how to detect and what can be the solution to the problem?

Nikki
  • 3,314
  • 13
  • 36
  • 59
  • Are you experiencing specific problems in your app? Are you leaking memory somewhere else, perhaps? Posting your code may help us to understand what you problem is, and a solution to it. – Mark Allison Jun 13 '11 at 07:13
  • @Mark Allison: On removing sensorEvent, I am not getting any problem of OutofMemery. – Nikki Jun 13 '11 at 07:21
  • In that case, I don't understand your problem. You're asking about memory issues, but you're not experiencing any memory issues. What exactly are you trying to fix? – Mark Allison Jun 13 '11 at 07:23
  • @Mark Allison: I am experiencing memory issues while using Sensor Event and if I remove Sensor Event then my code works fine – Nikki Jun 13 '11 at 07:30
  • Please post your code. It's almost impossible to work out why this may be happening if you don't post your code. – Mark Allison Jun 13 '11 at 07:41
  • @Mark Allison: Which code do you want where I have used sensor or what? Actually, my app is having 25MB of .apk file and using lots of images. For changing images,I am using setImageResource() and also using shaking at same time. So, read theques. carefully...I just want to know how much memory does sensor event takes and regitering again and again effect anything on memory usage – Nikki Jun 13 '11 at 08:00
  • 1
    It sounds like you are allocating memory on your event handlers which isn't getting released. That's why it is necessary to see your code. If that is not an option, then you're on your own, I'm afraid. All I can suggest is to look at http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html to try and determine where you are leaking memory. Alternatively, keep your Sensor Event handlers in place but comment out the body of your event handler methods. If the memory issue goes away, then it's clearly something that you are doing which is causing the problem. – Mark Allison Jun 13 '11 at 08:07
  • @Mark Allison: I have edited my question – Nikki Jun 13 '11 at 08:14
  • 1
    The only real problem that I can see is that you should really either registering in onCreate() and unregistering in onDestroy() OR registering in onResume() and unregistering in onPause(). The latter would be preferable if you have multiple activities doing this. But there's nothing which would explain why you would be leaking memory. – Mark Allison Jun 13 '11 at 08:22
  • @Mark Allison: Edited my question..........Hoping you could understand where the problem is? – Nikki Jun 13 '11 at 10:01

3 Answers3

1

The error is connected with Bitmaps not sensor.

  1. see OutOfMemoryError: bitmap size exceeds VM budget :- Android
  2. make sure that you call Bitmap.recycle()
  3. check size of the bitmap which you are loading
Community
  • 1
  • 1
bart
  • 2,378
  • 2
  • 19
  • 21
  • But I am not using bitmap. I am using imageview.setImageResource() for setting the image then how can I recycle the bitmap – Nikki Jun 13 '11 at 10:16
  • In that case please add full stack trace from LogCat (not only the name of exception) – bart Jun 13 '11 at 10:22
  • Getting error while using SensorEvent and same function of changing images works on button click and when using same function implemented with sensor event then getting outofmemory exception – Nikki Jun 13 '11 at 10:24
1

Your problem is in your GardenActivity.display() method where you are calling setImageResource. It is this that is causing your problem. Maybe check the size of the image that you're using, and reduce it.

To prove that this is correct, comment out the call to display() from within your onSensorChanged() method, and you'll find that it will work. Thus proving that it is not the sensor framework that's at fault, but it is something that you're doing within your event handler that's causing the problem (as I mentioned in my earlier comments).

Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • @Mark Allison : Can you please give an idea of how much big image does setImageResource() support. So, that it must not give this error and how to solve this problem – Nikki Jun 14 '11 at 04:00
  • There's not hard and fast rule, it depends on a number of factors such as how much RAM your device has, and what other processes are running. The issue that you should be considering is: How big is the image? If it is very large, can it be reduced in size? Also, make sure that you are not adding image objects to a collection and forgetting to remove them. – Mark Allison Jun 14 '11 at 06:14
  • @Mark Allison : How can we remove the imageview objects onPause or onDestroy() as I think it automatically gets destroy on these methods and I am also calling System.gc() in these methods. Is there any other way? – Nikki Jun 14 '11 at 06:28
  • You don't need to explicitly remove them unless, as I said, you are storing them in a collection somewhere. You should not need to call System.gc() either as Android will perform garbage collection automatically when it needs to free up memory. Can I ask: How big is your image (pixel dimensions, that is)? – Mark Allison Jun 14 '11 at 06:32
  • @Mark Allison : My images are of 320x260 dimensions and backgroung is of 960x480 dimension and also I have to use images for hdpi with different in same aspect ratio and what type of collection you are talking about. Can you give example of collection – Nikki Jun 14 '11 at 06:52
  • The background image could be a problem. Have you considered using a 9-patch or vector drawable, both of which can be significantly smaller? Also, how many images in total are you using? And how many at any one time? Are you storing them in a collection after you load them? – Mark Allison Jun 14 '11 at 06:55
  • @Mark Allison : what type of collection you are saying-- can you give some example as I am not able to understand this – Nikki Jun 14 '11 at 06:57
  • Any Java collection such as `ListView`, `HashMap`, an array, or anything which extends `java.util.Collection` – Mark Allison Jun 14 '11 at 07:00
  • I am using array but not storing images in that array. My array is of int type – Nikki Jun 14 '11 at 07:04
  • Well the problem lies somewhere in your use of images. It's almost impossible to determine the cause without a lot more information about how you are actually using images within your app. – Mark Allison Jun 14 '11 at 07:08
  • Thanks,Well! I am just trying to reduce the image size. Does size of app matters for this problem – Nikki Jun 14 '11 at 07:21
  • Not really, it's more how your app uses memory that is most likely the problem, in this case. – Mark Allison Jun 14 '11 at 07:22
  • How can I freeup some of the objects memory by myself? – Nikki Jun 14 '11 at 07:38
  • It's impossible for me to guess how your code may be misusing memory unless I can see it. – Mark Allison Jun 14 '11 at 07:40
  • But there could be anyway to destroy or freeup the resources... I just want to know that way – Nikki Jun 14 '11 at 07:49
  • I repeat: It is impossible for me to guess how your code may be misusing memory **or any other resources** unless I can see it. – Mark Allison Jun 14 '11 at 07:51
  • Now, my code is not showing error, I have set all ImageView to null onPause to freeup the memory.........Thanks, you are right I was not freeing memory used by imageview objects. Cheers! :) – Nikki Jun 14 '11 at 10:37
0

I would say you are trying to load an image in your ImageView that is too big to be decoded in memory. Try to see which drawable is causing the error when you are getting the exception (use a breakpoint in Exception handling code like:

resourceId = ...; (here you put the resource ID)
try {
  imageView.setImageResource(resourceId);
} catch (Exception e) {  // to be removed in production code
  Log.d("", "Failed to load resource: " + resourceId);
}

I don't think this has anything to do with the sensor listener.

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124