Skip to content Skip to sidebar Skip to footer

Memory Leak In Android When Trying To Send A Form With Image To Php Server

I have a memory leak in this file, I cannot find where exactly, but I think is the image around --> (Bitmap bm = BitmapFactory.decodeFile(filename)), I have tried many different

Solution 1:

Bitmaps are very big memory consumers. Having two loaded into memory could be the big issue. You should consider using BitmapFactory.Options when you decode a new bitmap. Also, you don't need bm2. Instead, replace that line with this:

bm = Bitmap.createScaledBitmap(bm, width, height, true);

Finally, if you have no other options, you can increase your app's heap size using the Application attribute android:largeHeap="true" in your AndroidManifest.xml. This option should not be needed - and should only be considered for extremely graphic-intensive applications.

EDIT

Another link you may find helpful for optimizing Bitmap usage: http://developer.android.com/training/tv/optimizing-layouts-tv.html#HandleLargeBitmaps

Solution 2:

Bitmaps use up a lot of memory is clear. You need to take care of the following to use bitmaps effectively

  • Remember that your png's in drawables are automatically resized based on screen size by android. And that takes up a lot of memory. Android does not resize images if it finds images of its needed size / scale in the correct drawable folder. So to start with copy all ldpi files to hdpi too. This will reduce your memory utilization by 40% atleast. I know how this sounds, but this is true, profile your app using debuggable=true in the manifest, and heap utilization using ddms. Make the change and run exactly the same scenario, you will notice the 40% reduction.
  • When you read your bitmap, scale it down. Dont use just CreateBitmap, since it will read the entire file and utilize a lot more memory. Instead use BitmapOptions and scale it down. To scale it down you first need to set your options, and then use this options as a parameter to your CreateBitmap call. Here is a nice link. Search more on stackoverflow you will find some more interesting responses.
  • If you have any files in your drawables that are 1024X512, scale them down. OR Create new files that are clear but smaller in size. Use these files for mdpi, delete ldpi. Use the 1024X512 for your hdpi folder.
  • Explore the possibility of using smaller files, sort by size and play around with it a bit. The graphical view on eclipse for xml files is really neat, and relatively bug free. Use it.
  • Edited : Dont forget to null your bitmap for garbage collection. This is most important.

Solution 3:

A few general hints:

  • As @Phil suggested, Bitmap objects tend to eat a lot of memory in Android. You should always use SoftReferences to hold bitmaps so that the OS can free the memory when needed.
  • You should also use the recycle() method to throw away transformation bitmaps (ie, your bm2 variable) when you are finished with them.
  • As paranoid as it sounds, setting Bitmaps to NULL when you are finished with them is a good practice to hint to the garbage collector that they can be collected. However, as a general rule calling the gc manually in Android either makes matters worse or has no effect.
  • And finally, profile your application using ddms!

Solution 4:

Can't say for sure, as I'm not the Java developer, but in .NET BitMap calss is IDisposable and is recommended to be recycled as soon as it is not in use. May be you should free your memory after BitMap loading?

Solution 5:

Yes A memory cache offers fast access to bitmaps at the cost of taking up valuable application memory. If LruCache can't resolve the problem you can try this:ImageManager ,in the class ImageManager, it has a method recycleBitmaps.

Post a Comment for "Memory Leak In Android When Trying To Send A Form With Image To Php Server"