I am looking at 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 the M开发者_开发知识库T equivalent would be Dispose, am I correct?
I won't need to call the:
[activeController release];
[super dealloc];
methods as they will be Garbage collected on Monotouch, is this also correct?
MonoTouch is garbage collected, so you do not need to worry about doing the deallocation yourself.
That being said, there are times when you are aware that you are keeping some large resources in memory and you want to assist the system by disposing the resources right away instead of waiting for the garbage collector to kick in.
This is when calling Dispose comes in handy: it releases the resources associated before the garbage collector has to. This is particularly important for large objects, like images, as images are stored on the unmanaged heap, while object references are stored in the managed heap.
What this means is that if you have a 8 megabyte image: 8 megabytes are stored in the unmanaged heap (managed by Objective-C) and 1 pointer (4 bytes) in the managed heap. As far as Mono's Garbage Collector is concerned, you are using 4 bytes, not 8 megs.
So it is times like this when you can assist the system by calling dispose: you know that the innocently looking "myImage" variable actually points to a large blob of memory.
Monotouch is garbage collected. Before an object is garbage collected, the destructor for the object is called.
Here's Microsoft's page about C# destructors. I don't know if there's more relevant documentation for destructors in Monotouch.
You don't need to call release or dealloc, they're taken care of by MonoTouch.
From Xamarin Documentation
http://docs.xamarin.com/ios/advanced_topics/api_design#When_to_call_Dispose
You should call Dispose when you need Mono in getting rid of your object. A possible use case is when Mono has no knowledge that your NSObject is actually holding a reference to an important resource like memory, or an information pool. In those cases, you should call Dispose to immediately release the reference to the memory, instead of waiting for Mono to perform a garbage collection cycle. Internally, when Mono creates NSString references from C# strings, it will dispose them immediately to reduce the amount of work that the garbage collector has to do. The fewer objects around to deal with, the faster the GC will run."
精彩评论