Hi, I am Arun Kumar Palani, Senior software engineer in Luxoft & Microsoft certified solution Architect - Associate level.
What is memory leak?
A memory leak in C# happens when an application repeatedly allocates memory but neglects to release it even after the memory has served its purpose. As a result, the application gradually uses more memory over time, which may finally result in the application crashing or ceasing to function. Programming mistakes include failing to deallocate objects that are no longer required or keeping references around for longer than necessary can result in memory leaks. Circular object references, improper usage of data structures like lists and dictionaries, and improper handling of exceptions are just a few causes of these mistakes.
Various memory leaks situations.
- Usage of static objects.
- A long running thread.
- Manually implementation of IDisposable pattern.
- Usage of unmanaged objects.
Let's discuss one by one in detail.
1. Usage of static objects: When we use static class, variables will be available throughout the application life cycle. We need to be very careful when we are using static variable/classes. This is due to the garbage collector not collecting static objects and everything they reference. Don't use static keywords if it is not necessary.
2. A long running thread: If we work on long running or infinite looping conditions the thread will hold the object for a long time. If the object is being held for a long time GC will think that the object is in use and won’t clean up the memory. But it’s not because of the infinite looping, we should be very careful on writing such cases in programming.
Always set a exit condition.
3. Caching: It is a very good strategy to improve the application performance. But if we are using caching in all areas of the application that will lead to over caching and will end up with the out of memory. It is always good to cache only the frequently used objects.
4. Usage of unmanaged objects: Handling OS file system is the one of the best examples of unmanaged objects. If we are going to use a file system, we should properly dispose of all the objects once the work is done. Otherwise, because they are not cleaned up by the GC, these will result in a memory leak.
5. Manually implementation of IDisposable pattern: Always use the Dispose function on a class that implements the IDisposable interface. Otherwise, a memory leak may occur. The "using" sentence, which calls the dispose method for each condition, is the best approach to accomplish this. If you are unable to use the "using" statement, remember to do it manually and to suppress the finalize method as it is not required.
Why suppress the finalize method?
If we are using IDisposable interface, we are manually going to clean the object using Dispose method. Finalize is the automatic memory management method called by garbage collector to remove the unused objects.
If you're facing memory leak problem in your application and how did you resolved, it? Please comment below.