Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does MonoTouch garbage collect?

Tags:

xamarin.ios

Are details of the MonoTouch garbage collection published anywhere? I am interested in knowing how it works on the iPhone. I'd like to know:

  • How often it runs, and are there any constraints that might stop it running.
  • Whether it is completely thread safe, so objects passed from one thread to another are handled properly, of if there are constraints we should be aware of.
  • If there is any benefit in manually calling the garbage collector before initiating an action that will use memory.
  • How does it handle low memory notifications, and running out of memory.

    Such information would help us understand the stacks and thread information that we have from application logs.

    [Edit] I've now found the information at Hans Boehm's site, but that is very generic and lists various options and choices the implementer has, including how threads are handled. Specific MonoTouch information is what I am wanting here.

  • like image 511
    mj2008 Avatar asked Apr 21 '11 10:04

    mj2008


    1 Answers

    The garbage collector is the same one used in Mono, the source code is here:

    https://github.com/mono/mono/tree/master/libgc

    It is completely thread safe, and multi-core safe, which means that multiple threads can allocate objects and it can garbage collect in the presence of multiple threads.

    That being said, your question is a little bit tricky, because you are not really asking about the garbage collector when you say "so objects passed from one thread to another are handled property , of if there are constraints that one should be aware of".

    That is not really a garbage collector question, but an API question. And this depends vastly on the API that you are calling. The rules are the same than for .NET: instance methods are never thread safe, static methods are thread safe by default. Unless explicitly stated in the API that they are not.

    Now with UI APIs like UIKit or CoreGraphics these are not different than any other GUI toolkit available in the world. UI toolkits are not thread safe, so you can not assume that a UILabel created on the main thread can safely be accessed from a thread. That is why you have to call "BeginInvokeOnMainThread" on an NSObject to ensure that any methods that you call on UIKit objects are only executed no the main thread.

    That is just on example.

    Check http://monotouch.net/Documentation/Threading for more information

    Low memory notifications are delivered by the operating system to your UIViewControllers, not to Mono's GC, so you need to take appropriate action in those cases.

    like image 131
    miguel.de.icaza Avatar answered Oct 05 '22 00:10

    miguel.de.icaza