JPG as background in Activity produces memory leak

my little App always crashed after the 5th or 6th screen-rotation. Two main layouts are defined, one for landscape one for portrait. After spending the whole day looking for the memory leak I found the problem: I had a JPG defined as background for the Main-Activity. After changing the JPG to a PNG the memory problem was solved.

The background was defined as drawable and was defined in main.xml.

The problem did not come when I tested the App under 2.2 (emulator) but with 2.3.1 (also emulator) the app crashed.

Can someone reproduce this problem?


Now I reduced my app to the following code:

package at.memtest;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;

public class MemTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override
protected void onDestroy() {
    recycleImages();
    super.onDestroy();

}

@Override
protected void onPause() {
    recycleImages();
    super.onPause();
}


private void recycleImages() {
    final LinearLayout theView = (LinearLayout) findViewById(R.id.main_layout);
    if (theView != null) {
        final BitmapDrawable background = (BitmapDrawable) theView.getBackground();
        if (background != null) {
            background.getBitmap().recycle();
        }
    }
}
}

Now the memory stays constant... Can you imaging that this is necessary to avoid memory leaks? As I said above - the image I use as a background is a PNG with about 220K.

Answers


I experienced the same problem. Instead of .jpg I used .png, that solved the problem.


Are you sure that it's the file format that's the problem? Try this out: http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

It might be that you're just not running out of memory as fast with the PNGs.


I had problems with a jpg background image making my app extremely slow on certain devices, too. Using a png didn't solve it for me though. I think it might have something to do with the high compression I chose for the image to save space which might have resulted in more effort for decompression at runtime.


Need Your Help

angularjs dom manipulation in a table

angularjs checkbox conditional-statements

I've checked as many questions as I can, and I've found some answers but I'm stuck on the last stretch. Most of what I've found covers how to turn things on with a checkbox, not how to turn everyth...

Dissect text file using delimiters piece by piece

xcode file-io delimited-text

I have a file that contains a bunch of info which is separated by delimiters. So my delimiters (in the plain text file) look something like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.