Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remember to call close() on all Realm instances even though I call close()

I'm seeing

Remember to call close() on all Realm instances. Realm /data/data/com.org.example/files/default.realm is being finalized without being closed, this can lead to running out of native memory. 

even though I've clearly called .close() after every call of getInstance(realmConfig).

Most of the time it looks like this:

Realm realm = Realm.getInstance(realmConfig);
try {
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(someData);
    realm.commitTransaction();
} catch (Exception e) {
    realm.cancelTransaction();
} finally {
    if (realm != null) {
        realm.close();
    }
}

And other times it looks like (using Rx Observable Pattern):

@Override
public void call(Subscriber<? super JsonElement> subscriber) {
    SomeJson someJson = Realm.getInstance(mRealmConfig).where(SomeJson.class)
                    .equalTo("type", type)
                    .equalTo("id", id)
                    .findFirst();

    if (someJson == null || !someJson.isDataValid()) {
        Logger.d(TAG, "Did not find value for id: " + id + " for type: " + type);
        subscriber.onNext(null);
        subscriber.onCompleted();
        Realm.getInstance(mRealmConfig).close();
        return;
    }

    JsonElement val = (new JsonParser()).parse(someJson.jsonString);
    subscriber.onNext(val);
    subscriber.onCompleted();
    Realm.getInstance(mRealmConfig).close();
}

Any idea why I'm getting these warnings still?

like image 309
shoke Avatar asked Dec 24 '22 05:12

shoke


1 Answers

Realm realm = Realm.getInstance(mRealmConfig);
// ...
Realm.getInstance(mRealmConfig).close();

The problem is here. You have acquired another Realm, and closed it, but you haven't closed the orignal realm.

You should use try-with-resources for all this:

try (Realm realm = Realm.getInstance(realmConfig)) {
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(someData);
    realm.commitTransaction();
} catch (Exception e) {
    realm.cancelTransaction();
}

[Note that realm could never be null at the point you were testing it, and if it was it would have triggered an NPE much further up.]

and

@Override
public void call(Subscriber<? super JsonElement> subscriber) {
    try (Realm realm = Realm.getInstance(mRealmConfig)) {
        SomeJson someJson = realm.where(SomeJson.class)
                    .equalTo("type", type)
                    .equalTo("id", id)
                    .findFirst();

        if (someJson == null || !someJson.isDataValid()) {
            Logger.d(TAG, "Did not find value for id: " + id + " for type: " + type);
            subscriber.onNext(null);
            subscriber.onCompleted();
            return;
        }

        JsonElement val = (new JsonParser()).parse(someJson.jsonString);
        subscriber.onNext(val);
        subscriber.onCompleted();
    }
}
like image 170
user207421 Avatar answered Dec 29 '22 10:12

user207421