I wrote a Parcelable class and out of curiosity I wrote some debug output whenever it is created and whenever the finalize() method is called. The Parcable is put as Extra into an Intent that is used to start an Activity.
Now I see the following: Whenever I launch the Activity, my Parcelable is instantiated once (by my code), then internally parceled once and instantiated via parceling twice and afterwards finalize() is only called for two of the three created Parcelable instances (and the two finalize calls happen immediately after the Activity has launched).
So on every Activity launch, the Parcelable is instantiated three times, but only two instances are freed.
I thought maybe the third one is collected at some time later by the Garbage Collector, but I forced garbage collecting through the Android Profiler many times and used the app intensively, but the my Parcelables are never deallocated. So I have one leaking Parcable per Activity launch.
Is there something I can do about that? Has anyone seen a similar behaviour? Is this a bug in Android?
Here's the stripped down code of my Parcable:
public class MyParcelable implements Parcelable {
private final static String TAG = "MyParcelable";
public MyParcelable() {
Log.d(TAG, "MyParcelable()");
}
@Override
protected void finalize() throws Throwable {
Log.d(TAG, "finalize()");
super.finalize();
}
// Parcelable:
protected MyParcelable(Parcel in) {
Log.d(TAG, "MyParcelable(parcel)");
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Log.d(TAG, "writeToParcel()");
}
public static final Creator<MyParcelable> CREATOR = new Creator<MyParcelable>() {
@Override
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
@Override
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
@Override
public int describeContents() {
return 0;
}
}
The output is something like that:
MyParcelable()
writeToParcel()
MyParcelable(parcel)
MyParcelable(parcel)
finalize()
finalize()