Getting rid of memory leaks
All memory leaks share a common root cause: an object isn’t released from memory because another object holds a strong reference to it. The GC checks if this object is still in use and, as a result, doesn’t remove it from memory.
Here are some of the most frequent scenarios that lead to memory leaks:
- Reference from a long-lived object: For example,
AppShell
exists for the entire duration of the application. If you create a property inAppShell
and store a reference to another page, that page will never be released. - Reference from a static property or field: Static properties maintain their values for the life of the application. If you pass an object to a static property, that reference will persist until you explicitly remove it or stop the application.
- Event handlers or function delegates: When you subscribe to an event, the instance containing the event handler is stored in the class that raises the event, creating a strong...