Total Page Preview:   000000000613

What is Garbage collection in C-Sharp in English and Hindi


Garbage collection in C-Sharp in Engilsh

Garbage collection in C# is the automatic process of freeing up memory that is no longer being used by a program. It is a built-in feature of the .NET Framework and helps to reduce the risk of memory leaks and other memory-related issues.

In C#, memory is managed by the Common Language Runtime (CLR), which is responsible for allocating and deallocating memory. When an object is created, the CLR allocates memory for it. When the object is no longer needed, the CLR frees up the memory so that it can be used for other objects.
Here is an example of garbage collection in C#:
In this example, we create a new object of type MyClass and use it by calling its DoSomething() method. Once we are done with the object, we set it to null, which means that it is no longer being used. Finally, we force garbage collection by calling the GC.Collect() method.
The GC.Collect() method tells the CLR to immediately free up any memory that is no longer being used. This is an expensive operation and should be used sparingly, as the CLR is designed to manage memory automatically. However, in some cases, it may be necessary to force garbage collection to ensure that memory is freed up in a timely manner.

Garbage collection in C-Sharp in Hindi

Garbage collection C# mein ek aisa automatic process hai jo program dvara istemal kiye gaye memory ko automatically free kar deta hai jab vah memory program dvara kisi object ke liye istemal nahi kiya jata hai. Ye .NET Framework ka ek built-in feature hai aur memory leaks aur dusre memory-related issues ko reduce karne mein madad karta hai.
C# mein memory Common Language Runtime (CLR) dwara manage kiya jata hai, jise object allocate karne aur deallocate karne ka jimmedari hai. Jab ek object create hota hai, CLR uske liye memory allocate karta hai. Jab object ko aur use nahi kiya jata, tab CLR memory ko free kar deta hai jisse ki vah dusre objects ke liye istemal kiya ja sake.
Neeche ek example hai jisme C# mein garbage collection ka use kiya gaya hai:
Is example mein, hum MyClass type ka ek object create karte hain aur uska use DoSomething() method ko call karke karte hain. Jab hum object se kaam khatam kar dete hain, hum use null mein set karte hain, jisse ki vah ab use nahi ho raha hai. Aakhri mein, hum garbage collection ko force karne ke liye GC.Collect() method ko call karte hain.
GC.Collect() method CLR ko immediately bata deta hai ki kaun si memory ab istemal nahi kiya jaa raha hai. Ye ek mehnga operation hai, isliye ye jyada use nahi kiya jana chahiye, kyunki CLR memory ko automatically manage karne ke liye design kiya gaya hai. Lekin kuch cases mein, garbace collection ko force karna zaruri ho sakta hai jisse ki memory timely manner mein free ho sake.

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn :





Facebook User: