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

Should release mColors in recycle? #46

Closed
SpecialCyCi opened this issue Mar 29, 2014 · 3 comments
Closed

Should release mColors in recycle? #46

SpecialCyCi opened this issue Mar 29, 2014 · 3 comments
Assignees

Comments

@SpecialCyCi
Copy link

It seems like that mColors has not been recycled by VM...

@koral--
Copy link
Owner

koral-- commented Mar 30, 2014

What do you mean by releasing? mColors is a Java variable so it is released automagically by GC. Only memory allocated at native (C) level should be explicitly released which happens in recycle() method.

@SpecialCyCi
Copy link
Author

Hi, koral. I have made two experimental projects to explain this problem.
https://github.com/SpecialCyCi/GifExperimentWithoutRecylemColors
https://github.com/SpecialCyCi/GifExperimentWithRecylemColors

My experimental environment:
SDK Android 4.3
IDE IntelliJ IDEA 13
Emulator 240 x 320 ldpi, Android 4.3, API 18 RAM 512M, VM Heap 16M

GifExperimentWithoutRecylemColors

The android-gif-drawable in GifExperimentWithoutRecylemColors didn't change any file. And app crashed when I click StartActivity2 button.

04-04 07:28:38.279    2478-2478/? I/dalvikvm-heap﹕ Clamp target GC heap from 16.130MB to 16.000MB
04-04 07:28:38.309    2478-2478/? D/dalvikvm﹕ GC_FOR_ALLOC freed 14K, 1% free 14386K/14508K, paused 109ms, total 114ms
04-04 07:28:38.309    2478-2478/? I/dalvikvm-heap﹕ Forcing collection of SoftReferences for 3145744-byte allocation
04-04 07:28:38.489    2478-2478/? I/dalvikvm-heap﹕ Clamp target GC heap from 16.121MB to 16.000MB
04-04 07:28:38.519    2478-2478/? D/dalvikvm﹕ GC_BEFORE_OOM freed 11K, 1% free 14374K/14508K, paused 201ms, total 202ms
04-04 07:28:38.539    2478-2478/? E/dalvikvm-heap﹕ Out of memory on a 3145744-byte allocation.

It seems like that mColors has not been recycled by VM.


GifExperimentWithRecylemColors

In this project, I just change the declaration of mColors and set mColors to null recycle() method

    GifDrawable.java
    .....
    protected int[] mColors;
    .....
    public void recycle() {
        mColors = null;
        mIsRunning = false;
        int tmpPtr = mGifInfoPtr;
        mGifInfoPtr = 0;
        free(tmpPtr);
    }

Run the app, click on StartActivity2.

04-04 07:36:33.249    2524-2524/? D/dalvikvm﹕ GC_FOR_ALLOC freed 11733K, 5% free 2668K/2792K, paused 120ms, total 142ms
04-04 07:36:33.329    2524-2524/? I/dalvikvm-heap﹕ Grow heap (frag case) to 5.687MB for 3145744-byte allocation
04-04 07:36:33.419    2524-2533/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 3% free 5738K/5868K, paused 81ms, total 82ms
04-04 07:36:33.549      284-300/? I/Choreographer﹕ Skipped 33 frames!  The application may be doing too much work on its main thread.
04-04 07:36:33.699    2524-2524/? I/Choreographer﹕ Skipped 100 frames!  The application may be doing too much work on its main thread.
04-04 07:36:34.739      284-300/? I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
04-04 07:36:34.889      284-300/? I/Choreographer﹕ Skipped 91 frames!  The application may be doing too much work on its main thread.
04-04 07:36:34.909      284-300/? I/ActivityManager﹕ Displayed com.example.GifExperiment/.Activity2: +2s268ms

mColors have been released by VM and Activity2 starts successfully.

@koral--
Copy link
Owner

koral-- commented Apr 8, 2014

OK, I see that explict releasing mColors will be helpful, but setting it to null is not enough since NPE can occur when some other method was called. We need to add nullchecks or replace mColors with 0-length array.

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

No branches or pull requests

2 participants