0

I would like ask you simple question. I fight with java.lang.outofmemory error. I t is caused probably by pictures but also I have in my project quite alot unused imports and unused variables at this moment (application with 7 activities and every activity aprox 40 variables).

Take unused imports and unused declared variables memory ? Do you think, can be java.lang.outofmemory error caused of many variables and imports ?

mira
  • 1,056
  • 3
  • 15
  • 32

2 Answers2

2

From Android:

Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

Basically, Images are a killer if not used properly.

See this android tutorial on Loading Large Bitmaps Efficiently

Specifically, the code examples to loading in scaled bitmaps from files/resources, at the required resolution.

Imports have no effect on the memory, at run time. The only thing they might do is slow down the build time. Nothing detrimental.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • Yes, but I have *.png pictures in size 20 kB, and size 200x200px (300x300 px) + one image on the background 700 x 700 px. The picture on the background uses translate animation after activity is started. I think the pictures are small and also my device is quite strong. Tablet Acer Iconia 500. Do you think animation can have some memory requirements. I think the pictures are quite small and this error is strange for me. – mira Dec 18 '12 at 18:45
  • @mira how many pictures of size 200x200 do you have? Animation shouldnt be a problem. A single 700x700 should be fine, although is still 2mb. – IAmGroot Dec 18 '12 at 19:15
  • From 4 to 12 pictures (different in each activity) + one background 700 x 700. Because of transparency I use png files and some backgrounds seems very large data to me, about 500-900 kB. But I didnt find sollution how to reduce them more. – mira Dec 18 '12 at 20:47
  • a bitmap representation of a png file is far bigger. 4 bytes per pixel. Im not sure exactly what it is your doing. If you are trying to create a master image from multiple images, one by one, open them, draw them to a master canvas then close them. So only 2 images are in memory at any one time. If you are showing 12 seperate images on screen, then it stands to reason that they should be scaled, to even fit 12 images on screen. The method `decodeSampledBitmapFromResource` and `calculateInSampleSize` from the link are used to scale images. – IAmGroot Dec 18 '12 at 21:38
  • Ooops thank you for your information. But it is little bit difficult to understand for me. All the images I have declared simple from XML. 6 images are and one is . Please have you any link on such example you write about to look on code to understand ? – mira Dec 19 '12 at 10:25
0

No, variables not occupy more memory in Heap of application. This may be due to bitmap in your application. If you are getting any error, make sure you have released images background like

imageview.setBackgroundDrawable(null);
relativeLayout.setBackgroundDrawable(null);

or

imageview.setBitmapImage(null);

This will remove drawable images used in layouts.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • Do you mean use this code in ondestroy() when I finish activity and go to next activity ? – mira Dec 18 '12 at 16:17
  • if you are moving from one activity to another activity, then place this code in `onStop()`, because whenever activity goes in background, `onStop()` will be called, and whenever you are comming back to your main activity `onResume()` will be called, so instead of placing any drawable in `layout`, place drawable by view's method `setBackgroundResource()` in `onResume()` method – Chintan Rathod Dec 18 '12 at 16:21
  • @ChintanRathod I think you mean, `onPause()`, rather than `onStop()`. – IAmGroot Dec 18 '12 at 16:25
  • @Doomsknight, onPause() will be called when another activity is called, but onStop() will be called when another activity finishes its loading. So if you put code in `onPause()` you will find that your screen first get black due to removal of image, and then another activity started, but if you write code in `onStop()` you will find no change happen in application. – Chintan Rathod Dec 18 '12 at 16:28
  • @ChintanRathod Sorry I thought you were trying to realise when hidden. and reinitalise `onResume()`. If your doing it `onStop` you should initalise `onStart` else you are repeating things in `onResume` when it only pauses. If that makes sense. - reintialising things that were never released. (Because `onStop` was never called) – IAmGroot Dec 18 '12 at 16:34
  • Now I have activity "one", then I load activity "two" and finally close activity "one" using finish(). This is why I used onDestroy(). Intent next = new Intent(MainActivity.this, MainActivity2.class); startActivity(next); finish(); – mira Dec 18 '12 at 18:50