A memory leak is a situation that occurs when a program or an application uses the system’s primary memory over a long period. When the program does not release the memory occupied by it during execution, even after it completes its execution process, then this allocated memory space degrades the system’s performance and can make it unresponsive. In this topic, we are going to learn about C# Memory Leak.
It is the responsibility of a garbage collector to free the unused allocated memory, but we still encounter the problem of memory leak because we sometimes refer the unused object from a variable that never goes out of scope throughout the application’s lifetime.
Syntax
There are many ways to avoid memory leak in C#; we can avoid memory leak while working with unmanaged resources with the help of the ‘using’ statement, which internally calls Dispose() method. The syntax for the ‘using’ statement is as follows:
using(var objectName = new AnyDisposableType) { //user code }
In the above statements, ‘var’ is the keyword which is used to store any type of data, and the compiler can figure out this data type at compilation time. ‘objectName’ is any user-defined name for the object. ‘new’ is the keyword used to initialize object and ‘AnyDisposableType’ can be any class like StreamReader, BinaryReader, SqlConnection, etc. whose object can be disposed of with the help of ‘using’ statement.
For .NET applications, we have a garbage collector to dispose of the unused memory, but still, we encounter the problem of memory leaks. This does not mean that the garbage collector does not work properly, but this occurs because of some ignorance by the programmer.
Suppose we ignore memory leaks in our application for a very long period. In that case, we increase our application’s memory consumption, which degrades the performance of our application and can gradually destroy it, giving the OutOfMemoryException.
There are two major causes for memory leak in C#:
Some reasons causing memory leak in C# are as follows:
Different examples are mentioned below:
This example shows a thread waiting for itself to terminate and thus can become a cause of memory leak.
Code:
using System; using System.Threading; namespace ConsoleApp4 { public class Program { public static void Main() { while (true) { Console.WriteLine("Press enter key to start new thread"); Console.ReadLine(); Thread thread = new Thread(new ThreadStart(StartThread)); thread.Start(); } } public static void StartThread() { Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + " started"); //Wait until current thread terminates Thread.CurrentThread.Join(); } } }
Output:
Whenever we press ‘Enter’ in the above program, its memory utilization increases.
Example showing the use of unmanaged resources with the help of ‘using’ statement to avoid a memory leak.
Code:
using System; using System.IO; namespace ConsoleApp4 { public class Program { public static void Main() { string filePath = @"E:\Content\memoryLeak.txt"; string content = string.Empty; try { //writing file using StreamWriter //making use of 'using' statement to dispose object after using it using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("Learning C# programming"); } //reading file using StreamReader using (StreamReader streamReader = new StreamReader(filePath)) { content = streamReader.ReadToEnd(); } } catch (Exception exception) { Console.WriteLine(exception.Message); Console.ReadLine(); } Console.WriteLine(content); Console.ReadLine(); } } }
Output:
Some points to keep in mind to avoid OutOfMemoryException due to memory leak in C# are as follows:
When an application doesn’t release the memory that it has used during its execution, this memory will be blocked and cannot be used by any other process, resulting in a memory leak. The garbage collector can automatically dispose of managed objects but cannot dispose of unmanaged objects or resources.
The above is the detailed content of C# Memory Leak. For more information, please follow other related articles on the PHP Chinese website!