Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How intelligent is the garbage collector?

pokrakam
Active Contributor
0 Kudos
153

Hi all,

I've got a question about how garbage collection works in an ABAP objects context.

If there are no more references an object is cleaned up and the memory released. Fine.

What about an object O1, which in turn creates another object O2. O2 however also contains a refernce to O1.

When the original process completes, this leaves two objects that reference each other, but with no other contact to the 'outside world'. Is the garbage collector intelligent enough to dump them?

Any input appreciated,

Cheers

Mike

1 ACCEPTED SOLUTION

Former Member
0 Kudos
58

hi Mike

What i Think about the scenario that you are talking about, is....

The garbage collector is not intelligent enough to dump the two objects inspite of having no refrence to the outside world...

What i know about the ABAP Objcets Garbage collector feature is <b>"<i>As soon as no more references point to an object, the Garbage Collector removes it from the memory</i>"</b>.

But in this case the objects are refrencing each other..

<b>Note: I might be wrong, Any Correction will be appriciated.</b>

Regards

Mithlesh

6 REPLIES 6

Former Member
0 Kudos
59

hi Mike

What i Think about the scenario that you are talking about, is....

The garbage collector is not intelligent enough to dump the two objects inspite of having no refrence to the outside world...

What i know about the ABAP Objcets Garbage collector feature is <b>"<i>As soon as no more references point to an object, the Garbage Collector removes it from the memory</i>"</b>.

But in this case the objects are refrencing each other..

<b>Note: I might be wrong, Any Correction will be appriciated.</b>

Regards

Mithlesh

0 Kudos
58

Hi Mithlesh and Mike,

In ABAP Objects, the objects you create are not accessed with its name but it is accessed with its reference. Let us take the same example which you referred to:

O1(which is a reference) object contains O2(which is again a reference) and O2 points O1(which are references but not objects--please remember). Very good!!! this still means there are two objects created and O1 and O2 are referring to them.

Now the garbage collector clears them when there is no variable referring to it. This means once the life of O1 and O2 expires or O1 and O2 refers to two new objects, the object is cleared.

When I say <b>life of variable</b>, that will totally depend upon the variable declaration. Say if it is locally declared in a sub-routine, then the variable expires once the subroutine is executed. If it is globally declared, it will wait till the program ends its part.

When I say new objects, thats when you create objects using the CREATE OBJECT o1/o2. Then two new objects are created and o1 and o2 are now referring to them as again I say they are only references. Now the previous two objects are flushed off.

Hope this helps.

Regards,

Srikanth

0 Kudos
58

Hi,

Thanks for the feedback. I am still not sure - you say 'when the program ends it's part'. This is exactly the point - when the program ends, do the objects remain because they still contain references to each other?

More detail:

The references are public instance attributes of both objects (different classes). I would prefer to use functional methods, but am stuck with public attributes which I'm populating in the constructor.

So a process P declares a local var LV_O1 and does a CREATE OBJECT LV_O1.

In the constructor of O1, it does a

CREATE OBJECT me->ATTR_O2 to populate it's attribute with a reference to an instance of O2.

O2 doesn't need to do a CREATE OBJECT, but assigns a ref to O1 to it's attribute ATTR_O1.

So now O1 and O2 refer to each other by public attributes:

O1->ATTR_O2 and O2->ATTR_O1.

The big question is what happens when process P ends and it's variable LV_O1 ceases to exist?

In case anyone's wondering about the overall wisdom of the design, they have to be public attributes (used in Workflow) and they have to refer to each other as instantiation may also happen the other way around.

Cheers

Mike

0 Kudos
58

Hi Mike,

There is a big catch here. Tell me one thing, when ATTR_O1 is a public attribute of O2 which is type ref to O1, how do you access public attribute without instantiation, i.e O2? Now going further, let us say you have not instantiated O2, then guess what O1 is a static attribute for O2.....

Let me not juggle with words. Let me make it more clear.

O1 Constructor creates object of O2(i.e ATTR_O2) which passes ATTR_01 which is a object of O1. How can you do this? This will go in an infinite loop. O1 constructor calls O2 and O2 must call the constructor of O1 to return its object.

Coming to the point of lv_o1 which I assume to be TYPE REF TO o1. The garbage collector will flush objects of o2 and o1 both, once lv_o1 looses its existence as o2 is just attribute of the object and is not a variable. ATTR_O2 exists only in the object which will never be referred to once if the existence of lv_o1 is expired.

Lastly if you really want to manage memory better just clear lv_o1 once you complete the process with it and this will allow the garbage collector to flush them all.

Regards,

Srikanth

0 Kudos
58

Hi Srikanth,

Thanks for your feedback that does kindof answer it. What you're saying is that even though the references may exist in memory, if no program owns them anymore the objects all get dumped?

To answer your question, I've been down the 'infinite loop' road already:

One way is an optional parameter type ref to O1 in the constructor of O2. If it's passed it gets assigned to O2->ATTR_O1, if not, CREATE OBJECT.

Another way with better encapsulation is for O2 to call a public static functional method in O1 instead of CREATE OBJECT. O1 checks a private static attribute (populated in the constuctor) to see if it exists and either returns that or does a CREATE OBJECT and returns a new instance of itself. So there will only ever be one instance (you can of course add a key into the whole scenario for multiples).

Same thing in reverse for O1 instantiating O2.

Cheers

Mike

0 Kudos
58

Hi Mike,

You are welcome. Now I understand where were you coming from. I have seen these kind of design in GOS manager where only one instance is created for the view manager I suppose.

They are handy when we have controls and you want to restrict only one control.

Regards,

Srikanth