Click here to Skip to main content
15,889,447 members
Everything / Garbage collection

Garbage collection

garbage-collection

Great Reads

by Dennis C. Dietrich
Clarifying how the .NET GC identifies objects to collect once and for all... hopefully...
by Ben Hall (failingfast.io)
An under-utilised setting that can offer substantial performance gains
by Vassili Kravtchenko-Berejnoi
Use of System.WeakReference for self-cleanable registration of (short-life) objects
by Nicolas DESCARTES
How to implement a simple garbage collector in C#?

Latest Articles

by Nicolas DESCARTES
How to implement a simple garbage collector in C#?
by Mark Pelf
Discussion about problems with Garbage Collector and possible alternative solutions
by Vassili Kravtchenko-Berejnoi
Use of System.WeakReference for self-cleanable registration of (short-life) objects
by Ben Hall (failingfast.io)
An under-utilised setting that can offer substantial performance gains

All Articles

Sort by Score

Garbage collection 

9 Aug 2013 by Dennis C. Dietrich
Clarifying how the .NET GC identifies objects to collect once and for all... hopefully...
7 Oct 2018 by Ben Hall (failingfast.io)
An under-utilised setting that can offer substantial performance gains
27 Jan 2020 by Vassili Kravtchenko-Berejnoi
Use of System.WeakReference for self-cleanable registration of (short-life) objects
30 Jan 2024 by Nicolas DESCARTES
How to implement a simple garbage collector in C#?
17 Feb 2015 by Sergey Alexandrovich Kryukov
Use garbage collection by knowing that it exists and by never touching it. Why? To start with, understand how it works: http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)[^].[EDIT]Also, this understanding should bring you to the understanding that memory leaks in managed...
13 Sep 2016 by matt warren
How to prevent .NET Garbage collections with the TryStartNoGCRegion API
15 Sep 2016 by matt warren
There is an update to this post, based on feedback I received.In my last post I talked about the techniques that the Roslyn team used to minimise the effect of the Garbage Collector (GC). Firstly I guess its worth discussing what the actual issue is.GC Pauses and LatencyIn early versions of
13 Jul 2015 by Jo Mua`mar
We all love the C#'s garbage collector. I know, It's perfectly great for 99.999% of application etc...I was wondering however if it would be possible in any way to manually destroy an object in C#, as I can in C++. What I mean is that it's memory will be freed, (of course providing there are...
27 Mar 2016 by Sascha Lefèvre
Quote:Others say that although this is important, they don't actually do much in terms of memory issues.When not using using-statements the CLR eventually comes around to cleaning up those objects but there's no guarantee on when this will happen. So it can lead to inconsistent behaviour. When...
15 Sep 2016 by matt warren
This series is an attempt to learn more about how a real-life “Garbage Collector” (GC) works internally, i.e., not so much “what it does”, but “how it does it” at a low-level.
25 Sep 2017 by Dave Kreskowiak
If you're looking at Task Manager to determine the amount of memory your app is using, DON'T! It's lying to you. What you see in Task Manager is how much memory is RESERVED for your app's execution, NOT how much it's actually using. The .NET CLR manages the execution of your application and...
5 Feb 2014 by Richard MacCutchan
See http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#gc()[^]. However it is still the virtual machine's decision as to whether it runs immediately or queues the request. In general you should not be concerned about it.
5 May 2014 by Er. Dinesh Sharma
Hi Experts,Why Garbage-Collection give one more life to object that have finalizer in .net.ThanksDinesh
5 May 2014 by Sergey Alexandrovich Kryukov
Interesting question. Resurrection is explained here: http://msdn.microsoft.com/en-us/magazine/bb985010.aspx[^].The quality of this Microsoft article is just a bit questionable: it refers to the concept of "global object" which does not exist in .NET (so you can consider it just as...
7 Feb 2015 by Sergey Alexandrovich Kryukov
No! Your example uses value object (as opposed ) which does not have to be collected. More exactly, it could be a stack variable, then it will be removed when the stack frame is removed (say, on method return), and this has nothing to do with allocation or deallocation, or this could be an...
13 Jul 2015 by Joezer BH
It's a good question! However I'am afraid there is no straightforward methodology.Theoretically, the GC uses the Win32 VirtualAlloc function to reserves a segment of memory, and releases segments back to the operating system (after clearing them of any objects) by calling the Win32...
15 Sep 2016 by matt warren
GC pauses and safe points
31 Oct 2018 by Kornfeld Eliyahu Peter
Java Garbage Collection Basics[^] How Garbage Collection Works - Java Enterprise Performance | Dynatrace[^] Garbage Collection in Java - GeeksforGeeks[^] and a few hundred other...
26 Jan 2019 by OriginalGriff
See here: Paging Through a Query Result | Microsoft Docs[^]
18 Nov 2019 by RickZeeland
Maybe these tips will be helpful: 8 Techniques to Avoid GC Pressure and Improve Performance in C# .NET - Michael's Coding Spot[^]
30 May 2021 by OriginalGriff
You are looking at a lot of complicated work there - it's not going to be a simple job at all. But this may help: Writing a Mark-Sweep Garbage Collector – Dmitry Soshnikov[^]
30 May 2021 by Greg Utas
Garbage collection normally isn't done in C++. Code is expected to free objects explicitly (e.g., by using delete) or implicitly (when a unique_ptr goes out of scope). However, here's an article[^] about a garbage collector that I wrote. Objects...
6 Jan 2014 by Parmod Jangra
When the garbage collector starts running, it makes the assumption that all objects in the heap are garbage. In other words, it assumes that none of the application's roots refer to any objects in the heap. Now, the garbage collector starts walking the roots and building a graph of all objects...
6 Jan 2014 by Ron Beyer
I'm not sure where you read that, but I would go through all these topics so you can fully understand .NET Garbage Collection:MSDN .NET Garbage Collection[^]If you don't read them all, at least read this one:.NET Garbage Collection Fundamentals[^]
5 Feb 2014 by Shah Parth P
How to ensure whether or not the garbage collector is called in java when we are using System.gc() ?
12 Jul 2014 by OriginalGriff
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.Try it yourself, you may find it...
8 Oct 2014 by Er Daljeet Singh
I searched from internet that garbage collector work or call in 3 generation ie.Generation 0Generation 1Generation 2and in each generation it perform 3 steps ie.CollectionCompaction/De-fragmentationReference UpdateI just to know how Garbage will understand that it is...
7 Jan 2015 by Bob_katt
I have created a form using multitouch with the Windows7.Multitouch.dll The form contains a Panel and 3 Buttons The buttons each populate the Panel with either 6 Red Pictureboxes with touch enabled or an animated gifThe Problem is when I leave the non touch page for the 2nd time I get an IDE...
7 Feb 2015 by Vivek S Kale
if I declared int a=2 , and it is not used anywhere in the code,does garbage collector will free it
7 Feb 2015 by Zoltán Zörgő
If you build as "release" instead of "debug", a variable that is declared but never used will be optimized on build, and actually not even put into the compiled code.
24 Feb 2015 by Am Gayathri
How do i dispose managed and unmanged code using Dispose method in C#?After reading some article i came to know that we need to use Dispose method to clear the garbage. See below codepublic class abc: IDisposable{ public void Dispose(); protected virtual void Dispose(bool...
24 Feb 2015 by Tomas Takac
First, you are not showing the whole implementation of IDisposable. You are missing two important pieces to the puzzle:1) Object's finalizer method2) The using statement[^]The full disposable pattern should look like this:public void Dispose(){ Disposing(true);}private...
27 Feb 2015 by Oleksandr Kulchytskyi
So with help of @manchanxwe reached next steps:1) The main culprit who has been released FileReader object and put it to the finalization stage was optimization that is turned on during Release and debug symbols are not emitted.That is why during the active workflow in the PerformRead...
9 Jul 2015 by Philippe Mori
If garbage collection is a problem for your application, then maybe you should not use .NET in that case. This is well known that managed code could have those delays. So for application like video editing or low-latency multi-track audio, native programming would generally be recommand..NET...
14 Jul 2015 by Hrishikesh Ranjit Shivacharan
There is a way to destroy the object in memory as follow.1. GC.Collect.2. IDisposable interface. For memory reclaim we can use both of this but when we use GC.collect in c# application is called bad way implementationIDisposable is a good way to destroy the object in memory.
27 Mar 2016 by Xenac
HelloI am trying to create a tile editor application that joins 16x16 sized png images into one large png file. I have tried this before when trying to develop other applications and have failed to get around the issue of memory leakage or just general GDI+ errors.Now I know this isn't...
24 Apr 2016 by OriginalGriff
Both: the Garbage Collector is part of the CLR and works to clean up after you and free memory. It is in control of the Heap and it's free memory lists, so it also does the allocation of memory: Automatic Memory Management[^]The system allocates the memory to start with when your app starts...
15 Jul 2016 by Dan_Suitor
We are experiencing periods of time when CPU utilization of our application goes to %100 percent for several seconds. I tracked this down to two CLR methods which are called when ‘new’ objects are created. The first being ?JIT_New@@YIPAVObject@@PAUCORINFO_CLASS_STRUCT_@@@Z. The second being...
15 Jul 2016 by Dan_Suitor
Per DebugDiag:The following threads in Zetta_160502_185436.dmp are waiting for .net garbage collection to finish. Thread 105 triggered the garbage collection.The gargage collector thread wont start doing its work till the time the threads which have pre-emptive GC disabled have finished...
15 Sep 2016 by matt warren
Measuring performance accurately is hard. But it is a whole lot easier if someone with experience takes the time to explain your mistakes to you! This is an update to my previous post.
21 Sep 2016 by OriginalGriff
No. Why would you expect us to help you cheat on a test / quiz / interview?The idea is to find out what you know, and what you are capable of, not us - so that the company can get an idea of what you could do as an employee. So get on with it and try it yourself instead of wasting your (very...
13 Mar 2017 by Dominic Abraham
Have you tried using the below method ?GC.GetTotalMemory(true) This you can use at start / end of the method and get the difference.RegardsDominic
22 Mar 2017 by Chaitali Patkar
Who calls GC.Collect() in C#? is it a CLR?What I have tried:I have tried to find the working of GC.Collect() but I didnt find any good article. Please help to understand the caller of the GC.Collect()
22 Mar 2017 by Suvendu Shekhar Giri
Quote:Who calls GC.Collect() in C#? is it a CLR?It's you (your code) who calls GC.Collect(), I belive.GC.Collect Method (System)[^]When to call GC.Collect() – Rico Mariani's Performance Tidbits[^]
22 Mar 2017 by OriginalGriff
In effect, you do: it's just done behind the scenes.When you create a new instance of a class, the space needs to be allocated on the heap. The allocation system looks for a suitable "chunk" of memory to use, and if it doesn't find one, it calls the garbage collector to try and free some...
3 Aug 2017 by Omprakash Sharma
I have Windows service which is coded in VB.Net. Service contains the main parent Thread which sleeps after interval of three minutes. When parent Thread start the execution it create multiple Child Threads whose task is to process the query and dump the data into MYSQL related tables. My...
3 Aug 2017 by Jochen Arndt
See Fundamentals of Garbage Collection | Microsoft Docs[^]: Quote: There is a managed heap for each managed process. All threads in the process allocate memory for objects on the same heap. So the garbage collection affects the memory from all threads of a process. If you run out of memory I...
25 Sep 2017 by tishi208
I am working on an application which is using WCF services. This service will get a call from some other application and will return the output to the caller. The response over here is in the form of concrete object which of type of a class. When this application gave response back to the...
25 Sep 2017 by Kornfeld Eliyahu Peter
There is no acceptable reason behind forcibly freeing the memory GC holds... (The only possible application when you may know better the state of memory then GC is a single-instance/single-user app... Service is definitely not one of them...) and... It is a very expensive operation... and... GC...
25 Sep 2017 by Graeme_Grant
Dave's article does a nice job explaining memory management. There is more to how the GC manages memory however is beyond the scope of this forum. A quick google search[^] should turn up lots of detailed articles that cover the finer points. The only thing that you need to really worry about...
1 Nov 2017 by Member 13409417
class Cardboard{ Short story = 200; Cardboard go(Cardboard cb){ cb = null; return cb; } public static void main(String args[]){ Cardboard c1 = new Cardboard(); Cardboard c2 = new Cardboard(); Cardboard c3 = c1.go(c2); c1 = null; } } What I have tried: According to me all 3...
1 Nov 2017 by Mehdi Gholam
After the main() code is run all objects are available for garbage collection. Garbage collections occur when the JVM hits memory limits, not necessarily immediately after a method completes. Setting an variable to null may not put that object up for garbage collection, garbage collection...
1 Nov 2017 by CPallini
Uhm.... Try: Short story = 200; Cardboard go(Cardboard cb){ cb = null; return cb; } public static void main(String args[]){ Cardboard c1 = new Cardboard(); Cardboard c2 = new Cardboard(); Cardboard c3 = c1.go(c2); c1 = null; System.out.println(c2.story); }...
2 Nov 2017 by Member 13409417
class beta{ } class demo{ static beta b1; beta b2; public static void main(String args[]){ beta b1 = new beta(); beta b2 = new beta(); demo d1 = new demo(); demo d2 = new demo(); d1.b1=b1; d1.b2=b1; d2.b2=b2; d1= null; b1= null; b2=null; } } If we add...
2 Nov 2017 by Richard MacCutchan
You have declared b1 as static so it always exists.
2 Nov 2017 by Mehdi Gholam
Please stop asking this type of question. As already stated all the objects are available for garbage collection after the main() method ends since the program terminates and there is no actively referenced objects in memory.
11 Jan 2019 by Member 11799596
I am creating a Bitmap object and from that Bitmap object creating an Icon and then Disposing them accordingly. While running the code I collected GDI object count after each GDI operation. Observed that after calling Dispose on the Bitmap object, GDI object count is still same. class...
12 Jan 2019 by RickZeeland
See answer here: c# - Icon.FromHandle: should I Dispose it, or call DestroyIcon? - Stack Overflow[^] For C# garbage collection, see: GC.Collect Method (System) | Microsoft Docs[^]
26 Jan 2019 by Member 14119435
Hello everybody. i have a problem i have a data base with 1M records. i want to fetch data from data base in memory Every thousand record by ADO.NET and do my operation then release the memory and fetch next thousand record and again ... do GC release memory in this condition? What I have...
18 Nov 2019 by Member 9903761
I have a vb.net application I developed that in the end I had to use GC.collect called at a specific point in the application cycle to avoid performance impact (whilst the task was at idle). This seemed to work for me. The question Is when I deployed, i ended up with multiple Applications...
30 May 2021 by temchik_ggg
I'm trying to understand how the garbage collector should work and maybe write it myself in C. I want to create my own stack and stuff variables there, as well as write a garbage collector for it. And I cannot understand how the program should...
30 May 2021 by KarstenK
For that you need to understand the concept of scope which means life time of an object. Here is some explanation of the scope in C++. You must also understand the differences between heap and stack memory in C++. You should read both sites...
9 Dec 2021 by codefast1993
I have a mixed mode dll (C++/Cli) that is referenced in a C# application. It crashes with the error: name_here.dll has caused an access violation exception(0xC0000005) when trying to read from memory location 0x00000000 on thread 243. What I...
11 Jul 2017 by Doug Duerner, Yeon-Chang Wang
In this article we explored a theory; would it be better to not utilize a heap construct when dealing with very large objects in .NET, in order to avoid Large Object Heap (LOH) fragmentation?
26 Apr 2022 by Mark Pelf
Discussion about problems with Garbage Collector and possible alternative solutions
17 Mar 2017 by Arthur Minduca
This post tells you about the absolute bare minimum that every programmer should know about memory management.
19 Aug 2018 by Ben Hall (failingfast.io)
This article is super-simplified, look at .NET garbage collection, with loads of intentional technical omissions. It aims to provide a baseline level of understanding that a typical C# developer realistically needs for their day-to-day work.
15 Sep 2016 by matt warren
As part of an ongoing attempt to learn more about how a real-life Garbage Collector (GC) works (see part 1) and after being inspired by Julia Evans’ excellent post gzip + poetry = awesome I spent a some time writing a tool to enable a live visualisation of the .NET GC in action.
16 Nov 2014 by Ronnie Mukherjee
This is a beginner's guide to garbage collection in .NET
8 Oct 2014 by Mehdi Gholam
Generations have different memory locations, read this : Fundamentals of Garbage Collection[^]
9 Jul 2015 by Sergey Alexandrovich Kryukov
You are not dealing with a real-time system, and you cannot directly and deterministically affect thread scheduling. You can only change priorities and… your design. You cannot really affect the scheduling of GC behavior. All you mention is completely out of control.At the same time,...
27 Feb 2015 by Oleksandr Kulchytskyi
Hi guys.Yesterday a junior developer from my department in our firm provided me with code, and asked me a question.I know that the code snippet has a lot of violations, I've pointed them to the junior:improper implementation of the Dispose patternusage of managed objects in .NET...
13 Jul 2015 by Wendelius
As far as I know all housekeeping operations go via garbage collector. So the closest thing that comes to mind is to call the Collect method. See GC.Collect Method[^]However, it should be needed in very rare occasions since the framework tries to keep enough free memory available.
8 Jul 2015 by Mehdi Gholam
Start here : http://www.toptal.com/c-sharp/top-10-mistakes-that-c-sharp-programmers-make[^]https://channel9.msdn.com/posts/YOW-2012-Martin-Thompson-Mechanical-Sympathy-and-High-Performance-Coding[^]and my hero Martin Thompson : http://mechanical-sympathy.blogspot.com/[^]
17 Feb 2015 by Abhinav S
To more about the Garbage Collector, go through Garbage Collection in .NET[^] and many other pages available on the internet.You cannot do much with GC through managed code. Check the dispose method for marking objects as ready to delete - Dispose method[^].
8 Jul 2015 by Joezer BH
In C#.NET, I need an application that will run smoothly. I have many serial chunks of computations I need to consecutively perform in short periods of time each, so I don't mind the GC doing it's job and I even can take more frequent collections but what I need to minimize is the length of each...
24 Apr 2016 by praveen iThesisArt
If I make any object for example :Emp obj= new Emp();So who clr , system(OS) or garbage collector will allocate memory to obj object in heap ?1. Extract 1 :Which concludes garbage collector is responsible.From micorsoft link -
31 Oct 2018 by Member 14029516
What’s the process that’s accountable for doing that in Java? What’s Garbage Collection? What I have tried: What’s the process that’s accountable for doing that in Java?
17 Feb 2015 by Am Gayathri
What is garbage collection in .net ?What is the use of that?Should we use it in all our code\projects?How to do it in code?Please get me some code real life example ?
12 Jul 2014 by Am Gayathri
generation-0 and generation-1 garbage collection combined also called as ??Please give me answer as soon as possible.
13 Mar 2017 by MK-Gii
Anyone have any ideas if there is a simple way to measure how much memory a certain part of application is consuming?For example I got an application that load text files, processes these and takes another one. I need to measure how much memory the single file processing took.I know I could...