Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin garbage collector and circular references

While reading Xamarin docs under section "Performance", I've noticed the following chapter:

The following diagram illustrates a problem that can occur with strong references:

Circular reference

Object A has a strong reference to object B, and object B has a strong reference to object A. Such objects are known as immortal objects due to the presence of the circular strong references. This parent-child relationship is not unusual, and as a result, neither object can be reclaimed by the garbage collector, even if the objects are no longer in use by the application.

That's the first time I've heard of "immortal objects" in C#/.NET/Mono context.

The page then continues with a suggestion to use a WeakReference in one of the objects, which will remove the strong circular reference and fix this "issue".

At the same time, Xamarin docs on garbage collection claim that:

Xamarin.Android uses Mono's Simple Generational garbage collector. This is a mark-and-sweep garbage collector [...]

Aren't mark and sweep GCs unaffected by circular references?

like image 973
Lou Avatar asked Feb 18 '16 20:02

Lou


People also ask

Does garbage collector handle cyclic references?

yes Java Garbage collector handles circular-reference! How? There are special objects called called garbage-collection roots (GC roots). These are always reachable and so is any object that has them at its own root.

How do you handle circular references in C#?

To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references. The garbage collector begins with local and static and it marks each object that can be reached through their children. Through this, you can handle the issues with circular references.

How does garbage collector remove unused references?

During the garbage collection process, the collector scans different parts of the heap, looking for objects that are no longer in use. If an object no longer has any references to it from elsewhere in the application, the collector removes the object, freeing up memory in the heap.

Can we force garbage collector to run in C#?

You can force garbage collection either to all the three generations or to a specific generation using the GC. Collect() method. The GC. Collect() method is overloaded -- you can call it without any parameters or even by passing the generation number you would like to the garbage collector to collect.


1 Answers

The memory leaks due to circular references apply only for Xamarin.iOS, wich use reference counting for native objects.

The page about immortal objects also says:

Boehm – This is a conservative, non-generational garbage collector. It is the default garbage collector used for Xamarin.iOS applications that use the Classic API.

The second quote specifically talks about Xamarin.Android.

like image 173
bwt Avatar answered Sep 19 '22 14:09

bwt