What is the equivalent of Monotouch dealloc?

I am watching an example online that contains this code in objective-c

-(void)dealloc { [activeController viewWillDisappear:NO]; [activeController.view removeFromSuperview]; [activeController viewDidDisappear:NO]; [activeController release]; [super dealloc]; } 

I assume that the equivalent of MT will be Dispose, am I right?

I will not need to call:

  [activeController release]; [super dealloc]; 

since they will collect garbage on Monotouch, is that right too?

+6
c # objective-c iphone
source share
4 answers

MonoTouch is garbage collection, so you donโ€™t have to worry about making the release yourself.

Speaking of this, there are times when you know that you are storing some large resources in memory, and you want to help the system by immediately saving resources, rather than waiting for the garbage collector to attack.

This is when the Dispose utility is called: it frees up resources associated before the garbage collector should. This is especially important for large objects, such as images, because images are stored in an unmanaged heap, and object references are stored in a managed heap.

This means that if you have an 8 megabyte image: 8 megabytes are stored in an unmanaged heap (managed by Objective-C) and 1 pointer (4 bytes) in a managed heap. As for the Mono Garbage Collector, you are using 4 bytes, not 8 megabytes.

So, the time has come when you can help the system by calling dispose: you know that the innocently looking variable myImage actually points to a large amount of memory.

+14
source share

Monotouch collects trash. Before the object collects garbage, the object's destructor is called.

Here is the Microsoft page on C # destructors . I don't know if there is more relevant documentation for destructors in Monotouch.

+1
source share

You do not need to call release or dealloc, they will be taken care of by MonoTouch.

+1
source share

From the Xamarin documentation

http://docs.xamarin.com/ios/advanced_topics/api_design#When_to_call_Dispose

You must call Dispose when you need Mono to get rid of your object. A possible use case is when Mono does not know that your NSObject really contains a link to an important resource, such as memory or an information pool. In such cases, you should call Dispose to immediately release the memory reference, rather than waiting for Mono to complete the garbage collection cycle. Internally, when Mono creates NSString links from C # strings, it will destroy them immediately to reduce the amount of work that the garbage collector needs to do. The fewer objects around, the faster the GC will work.

0
source share

All Articles