thomas farrell
thomas farrell

Reputation: 51

Quick Java garbage collection question

I am writing an alarm app in Android, and I have the following:

ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>();

public PendingIntent setAlarm(long time) {
    ...other code.
    PendingIntent pi = PendingIntent.getBroadcast(context, num, intent, flags);
    return pi;
}

I am wondering if doing this below multiple times, is the original Pending Intent reference overridden each time?

pendingIntents.add(num, setAlarm(1000));

Upvotes: 4

Views: 161

Answers (2)

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

No, it's not.

Each time you call add you insert an element after the specified index.

Perhaps you've meant to use set. That one replaces item at the num position and the old value becomes eligible for GC.

Upvotes: 2

Nishant
Nishant

Reputation: 55856

NO

Basically, you are temporarily assigning to the object reference to variable pi. The variable is overwritten, but the object is not. It safely gets added to your list for any future use.

Upvotes: 2

Related Questions