Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose we have a language using the reference model ie all variables are refere

ID: 3724479 • Letter: S

Question

Suppose we have a language using the reference model ie all variables are references, there is no explicit dereference operator). Memory is freed by a reference-counting garbage collector. Consider the following program. State which variables point to which objects after the execution of each line of the program and what the values of the reference counters of each object are (you can show only the information that changed during the execution of the line). Which objects can be garbage-collected after the execution of the last line? type C = type D = record {C f; ); C c1 = new C; C c2: D d1 = new D; D d2 = new D; % 01 is allocated (and cl initialized to point to ol) % (e2 is initialized to null) % o2 is allocated (and d1 initialized to point to 02) % 03 is allocated d1.f = c1; c2 = c1; c1 new C; d1.f = c1; d2.f = c2 ; e2 = null; % 04 is allocated

Explanation / Answer

The table below gives the changes in pointers and the reference count changes after each line of code.

In the last line o3 reference count becomes 0, which means that the object can be collected by garbage collector. All references that are contained within this object will accordingly reduce by 1. Therefore, since o3.f pointed to o1 prior to this line, the reference count of o1 also reduces by 1.

Objects o1 and o3 have a reference count of zero after the last line, therefore, o1 and o3 can be garbage collected.

C c1 = new C; o1 is allocated, c1 points to o1, reference count of o1 is 1 C c2; c2 is initialized to null D d1 = new D; o2 is allocated, d1 points to o2, reference count of o2 is 1 D d2 = new D; o3 is allocated, d2 points to o3, reference count of o3 is 1 d1.f = c1; o2.f points to o1, reference count of o1 is 2 c2 = c1; c2 points to o1, reference count of o1 is 3 c1 = new C; o4 is allocated, c1 points to o4, reference count of o4 is 1, reference count of o1 is 2 (decremented by 1) d1.f = c1; o2.f points to o4, reference count of o4 is 2, reference count of o1 is 1 (decremented by 1) d2.f = c2; o3.f points to o1, reference count of o1 is 2 c2 = null; c2 set to null, reference count of o1 is 1 (decremented by 1) d2 = d1; d2 points to o2, reference count of o2 is 2, reference count of o3 is 0 (decremented by 1), reference count of o1 is 0 (decremented by 1, due to o3 reference count becoming zero)