Java garbage collection

Exposure:

In general, link counting has the weakness of "it cannot detect loops." However, in some cases, link counting is really useful:

class EmergencyPatient {
  DoctorPtr doctor;
  EmergencyPatient() { doctor = Doctor::acquire(); }
  ~EmergencyPatient() { Doctor::release(doctor); } 
};

Now, in a reference-counting world, as soon as we no longer refer to the EmergencyPatient, the doctor is freed.

In the unrealized world of Java, this largely depends on when the EmergencyPatient collects garbage - and since the garbage collector is a generation, the EmergencyPatient can be in the older generation and not be collected for a long time.

Problem:

For me, a doctor is a very valuable resource; other EmergencyPatient need doctors. However, for Java, the EmergencyPatient object is just a few bytes of memory.

Question:

? ( , , , ).

!

+3
4

4 , , , " ". , "" GC, , , .

, , .

0

Java - try-finally:

Doctor doctor = doctorFactory.acquire();
try
{
    EmergencyPatient  patient = new EmergencyPatient(doctor);
    doctor.examinePatient();
}
finally
{
    doctor.release();
}

, , ( !) . .


: , , java.sql.Connection, java.io.InputStream , .

+4

( Doctors) . , , , Java , . , , , , , .

, , , , , , , , , . - , .

+3

. , GC . , , .., , .

0

Source: https://habr.com/ru/post/1727544/


All Articles