Forum Xamarin.Forms

Releasing resources when a Page is destroyed

ZebeZebe Member ✭✭

Hello,

I have seen various practices for releasing resources when a page dies, yet I do not completely grasp how many of them are necessary practices, “extra” precautions or “hacks” from another (more buggy) era of forms.

My understanding: Events and Behaviors must be released when a page is destroyed, in the case of the event this allows the subscriber script to be garbage collected. Since I now use Prism I can do that with the IDestructible interface. I have seen other good implementations here on the forums for the MVVMLight that I used before Prism, but the concept is the same, a routine to be called on destroy where you can release such things.

I have seen the practice of releasing the BindingContext of a ContentPage by setting it to null on the code behind, is this a valid practice? Is it necessary?

I have also seen the practice of releasing images of a ContentPage on the code behind by setting their source to null. Aren’t images released when the script is garbage collected?

I create a lot of custom controls that are either ContentViews or a plain Layout, but the IDestructible (or other non Prism implementations) is not called for them when the page dies. Basically these controls are the View and its Code Behind for the BindingProperties and Gestures recognizers. Usually the code behind is set as the View’s binding context and the view binds to its public properties. Another page then binds those BindingProperties (as it would on any other forms control) to its ViewModel's properties. Do I have a memory leak here?

Sadly, I cannot measure any of the above (or memory leaks), because having the profiler requires the Enterprise license which is, well, expensive. I would not ask questions I could find the answer to myself, even with experimenting with the above cases and just taking notes on the results.

Any knowledge that you have gathered and can share is much appreciated, this is a subject I do not know how to even google for. I’ve been looking around to the Performance category on the docs, cannot say that these are practices that are addressed there directly. Thank you in advance for any answer!

Sign In or Register to comment.