ZhouWeikuan / cocos2d

cocos2d for android, based on cocos2d-android-0.82, and now ported from cocos2d-iphone 0.99.4. The googlecode address is here: http://code.google.com/p/cocos2d-android-1/ . There are several demos to watch.
610 stars 291 forks source link

Bitmap exceeds VM budget #5

Closed b0ktai closed 13 years ago

b0ktai commented 13 years ago

I had been banging my head to the wall with this error when loading multiple sprites. I solved it by recycling the bitmap after it was used:

bmp.recycle(); bmp = null; is = null;

The modified function was the following, in CCTextureCache class:

private static CCTexture2D createTextureFromFilePath(final String path) {

    final CCTexture2D tex = new CCTexture2D();
    tex.setLoader(new GLResourceHelper.GLResourceLoader() {

        public void load() {
            try {
                InputStream is = CCDirector.sharedDirector().getActivity().getAssets().open(path);
                Bitmap bmp = BitmapFactory.decodeStream(is);
                is.close();
                tex.initWithImage(bmp);
                bmp.recycle();
                bmp = null;
                is = null;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    return tex;
}

Can you confirm this bug, or if there is another workaround I am not aware off.

This error seems to happen because GC doesn´t free memory at the needed rate...

ZhouWeikuan commented 13 years ago

yes, it is a bug, and we have to save bmp without recycling it to restore when the app is back and we have to load everything for it.

Thanks, Weikuan Zhou

2011/4/12 b0ktai < reply@reply.github.com>

I had been banging my head to the wall with this error when loading multiple sprites. I solved it by recycling the bitmap after it was used:

bmp.recycle(); bmp = null; is = null;

The modified function was the following, in CCTextureCache class:

private static CCTexture2D createTextureFromFilePath(final String path) {

   final CCTexture2D tex = new CCTexture2D();
   tex.setLoader(new GLResourceHelper.GLResourceLoader() {

                   public void load() {
               try {
                           InputStream is =

CCDirector.sharedDirector().getActivity().getAssets().open(path); Bitmap bmp = BitmapFactory.decodeStream(is); is.close(); tex.initWithImage(bmp); bmp.recycle(); bmp = null; is = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } });

   return tex;

}

Can you confirm this bug, or if there is another workaround I am not aware off.

This error seems to happen because GC doesnt free memory at the needed rate...

Reply to this email directly or view it on GitHub: https://github.com/ZhouWeikuan/cocos2d/issues/5

opengenius commented 13 years ago

recycle is called in loadTexture method of CCTexture2D now.

b0ktai commented 13 years ago

I may be confusing something here, but I don't understand the load thing. If the load you're mentioning is the onStart() event of the activity, then there is no need to load the layer again. If the activity enters an onStop() state there is no need to call CCDirector.sharedDirector().end(); so when the activity comes back up there's no need to load anything again...

In my understanding I only call CCDirector.sharedDirector().end(); at the onDestroy event and nowhere else...

b0ktai commented 13 years ago

Sorry didn't see you're last post.

Thank you.