Search code examples
javac#androidxamaringarbage-collection

Does Immortal Object really exist?


In this article: Xamarin - Use Weak References to Prevent Immortal Objects It says that if two objects strong reference each other, they will become imortal, meaning the GC cannot collect them, even if the objects are no longer in use by the application.

However, in these 2 SO discussions:

Garbage collector and circular reference

Circular References Cause Memory Leak?

It seems that circular reference can be handled by GC, as long as the objects are no longer in use by the application.

Do I misundersand anything? Does Java's GC behave differently on Android? I am really confused. Any comments are appriatiated. Thanks!


Solution

  • I did some researches and want to answer my own question. Below are some findings:

    • In both Java and C# world, circular reference is not a problem. GC can handle that very well. Therefore, for Xamarin Android, that article is irrelevant and misleading. However that article is listed under cross-platform...
    • In Obj-C or Swift world, they use reference counting. And circular references will indeed cause memory leak. So in Xamarin.IOS we need to be careful about that. UPDATE: Thanks to PeterDuniho's comment, to be accurate, for Xamarin.IOS, the managed world is governed by Mono, we need not worry about circles. But if two native objects strong reference each others, they will become immortal... 😱😱😱

    I think that's it. Link some references on this topic: