Exposition:
In general, Reference Counting has the weakness of "it can't detect loops." However, in some cases, Reference Counting is really useful:
class EmergencyPatient {
DoctorPtr doctor;
EmergencyPatient() { doctor = Doctor::acquire(); }
~EmergencyPatient() { Doctor::release(doctor); }
};
Now, in a reference counted world, as soon as we no longer refer to an EmergencyPatient, the doctor is released.
In Java's non-refcounted world, this depends largely on when the EmergencyPatient is garbage collected -- and since the garbage collector is generational, the EmergencyPatient can be in an older generation, and not collected for a long time.
Problem:
To me, a doctor is a very precious resource; other EmergencyPatient need doctors. However, to Java, the EmergencyPatient object is just a few bytes of memory.
Question:
What is the right way to solve this problem? (There are some resources that I want to be freed as soon as I know they're no longer being used).
Thanks!