Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Your project has OOM problem #1

Open
huanzhiyazi opened this issue Jul 3, 2013 · 4 comments
Open

Your project has OOM problem #1

huanzhiyazi opened this issue Jul 3, 2013 · 4 comments

Comments

@huanzhiyazi
Copy link

hi, i tested this project, which has a good implementation of shake detection! But i'm so confused that didn't you discover your project can easily lead to OOM (Out of Memory)? As the ddms showed, it is likely to be caused by using bitmap inefficiently.

@jarofgreen
Copy link
Member

Thanks, hope you find it useful!

I will look at the memory issues later - what do you recommend for better bitmap handling?

@huanzhiyazi
Copy link
Author

In my opinion, you should recycle the old bitmap when a new one has been produced. Modification in MainActivity just like below:

Bitmap bit; //a member of the activity
...

public void nextPicture() {

            ... ...
    if (bit != null) {
        bit.recycle();
    }
    bit = BitmapFactory.decodeFile(images.get(next));
    mainImageView.setImageBitmap(bit);

}

other suggestions:

  1. use decodeFileDescriptor replaced decodeFile directly.
  2. it could be a better idea to push "decoding bitmap from file" into a work thread.

wish it can help you.

@jarofgreen
Copy link
Member

Thanks for the suggestions - I have used .recycle() and an Async task.

What's the advantage of using decodeFileDescriptor instead of decodeFile?

@huanzhiyazi
Copy link
Author

If you read the source code, it is clear that using decodeFile() is nearly equal to using decodeStream(), which is called by decodeFile() directly.

However, decodeFile()/decodeStream() and decodeFileDescriptor() call different nativeDecodeXXX method.

To someone's view, the nativeDecodeXXX used by decodeFileDescriptor() can consume less memory than the one called by decodeFile()/decodeStream(). I am not so sure if it is right. You can refer to this: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object

Another referable fact is an example from google developers: http://developer.android.com/training/displaying-bitmaps/index.html, you can download the demo project(BitmapFun), and what you can find clearly is that google developers avoid using decodeFile()/decodeStream() but instead use decodeFileDescriptor().

hope it is usable, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants