Due to memory leak problem, I have cleared my objects after leaving my component page, where I have called the dispose
for NSDate variable.
My question is,
Should we call dispose
for NSDate variable or it will automatically dispose
while leave the page. Please share your suggestion.

- 3,748
- 2
- 30
- 55

- 35
- 6
2 Answers
As you can see the lifecycle of an object written in
https://developer.xamarin.com/api/type/Foundation.NSObject/#Lifecycle
When you create an object from C# using the "new" operator, the object will initially be owned by C#, and C# will retain a reference to the object. This reference will only be dropped when the garbage collector determines that there are no pending managed references to it,or when you manually call the Dispose method on the object.
Objects will be cleaned up automatically by garbage collector when there are no pending managed references to it.
Back to you question, you know that NSDate inherits from NSObject,so when you create a instance of NSDate, the instance's lifecycle is managed by garbage collector. When you leave your page, the instance will no longer be used and it will be cleaned up by garbage collector automatically.
So,you can call dispose method to clean up it but overall there is usually no need to do that.
Here is a similar issue link that might help you:

- 40,302
- 20
- 199
- 253

- 15,432
- 1
- 12
- 30
garbage collector only Collect the C# based Variable.but the Nsobject will not collect by the garbage collector.so we have to remove manually. By using the dispose method.

- 254
- 2
- 11