Garbage Collection in .NET - How it really works
来源:互联网 发布:小意思tpo激活码 mac 编辑:程序博客网 时间:2024/05/14 13:47
Garbage Collection basis
- Almost every program uses resources such asdatabase connection, file system objects etc. In order to make use of these things some resources should be available to us.
- First we allocate a block of memory in the managed memory by using the new keyword.
- Use the constructor of the class to set the initial state of the object.
- Use the resources by accessing the type's members
- At last CLEAR THE MEMORY
These two are the serious bugs, which will lead us to memory leak and commonly occurring. In order to overcome these things the concept of automatic memorymanagement has come. Automatic memorymanagement or Automatic garbage collection is a process by which the system will automatically take care of the memory used by unwanted objects (we call them as garbage) to be released. Hurrah... Thanks to Microsoft's Automatic Garbage collection mechanism.
Automatic Garbage Collection in .NET
When Microsoft planned to go for a new generation platform called .NET with the new generation language called C#, their first intention is to make a language which is developerfriendly to learn and use it with having rich set of APIs to support end users as well. So they put a great thought inGarbage Collection and come out with this model of automaticgarbage collection in .NET.
They implemented garbage collector as a separate thread. This thread will be running always at the back end. Some of us may think, running aseparate thread will make extra overhead. Yes. It is right. That is why the garbage collector thread is given the lowest priority. But when system finds there is no space in the managed heap (managed heap is nothing but a bunch of memory allocated for theprogram at run time), then garbage collector thread will be givenREALTIME priority (REALTIME priority is the highest priority inWindows) and collect all the un wanted objects.
How does Garbage collector locate garbage
When an program is loaded in the memory there will be a bunch of memory allocated for that particularprogram alone and loaded with the memory. This bunch of memory is called Managed Heap in .NET world. This amount of memory will only be used when an object is to be loaded in to the memory for that particularprogram.
This memory is separated in to three parts :
- Generation Zero
- Generation One
- Generation Two
Ideally Generation zero will be in smaller size, Generation one will be in medium size and Generation two will be larger.
When we try to create an object by using NEW keyword the system will,
- Calculate the number of bytes required for the object or type to be loaded in to the managed heap.
- The CLR then checks that the bytes required to allocate the object are available in the reserved region (committingstorage if necessary). IF the object fits, it is allocated at the address pointed to by
NextObjPtr
. - These processes will happen at the Generation zero level.
When Generation Zero is full and it does not have enough space to occupy other objects but still theprogram wants to allocate some more memory for some other objects, then the garbage collector will be given the REALTIME priority and will come in to picture.
Now the garbage collector will come and check all the objects in the Generation Zero level. If an object's scope and lifetime goes off then the system will automatically mark it forgarbage collection.
Note:
Here in the process the object is just marked and not collected. Garbage collector will only collect the object and free the memory.
Garbage collector will come and start examining all the objects in the level Generation Zero right from the beginning. If it finds any object marked forgarbage collection, it will simply remove those objects from the memory.
Here comes the important part. Now let us refer the figure below. There are three objects in the managed heap. If A and C are not marked but B has lost it scope and lifetime. So B should be marked forgarbage collection. So object B will be collected and the managed heap will look like this.
But do remember that the system will come and allocate the new objects only at the last. It does not see in between. So it is the job of garbage collector to compact the memory structure after collecting the objects. It does that also. So the memory would be looking like as shown below now.
But garbage collector does not come to end after doing this. It will look which are all the objects survive after the sweep (collection). Those objects will be moved toGeneration One and now the Generation Zero is empty for filling new objects.
If Generation One does not have space for objects from Generation Zero, then the process happened in Generation Zero will happen in Generation one as well. This is the same case withGeneration Two also.
You may have a doubt, all the generations are filled with the referred objects and still system or ourprogram wants to allocate some objects, then what will happen? If so, then theMemoryOutofRangeException
will be thrown.
Dispose
Instead of declaring a Finalizer, exposing a Dispose method is considered as good.
public void Dispose(){// all clean up source code here..GC.SuppressFinalize(this);}
If we clean up a object, using Dispose or Close method, we should indicate to the runtime that the object is no longer needed finalization, by callingGC.SuppressFinalize() as shown above.
If we are creating and using objects that have DisposeorClosemethods, we should call these methods when we’ve finished using these objects. It is advisable to place these calls in a finally clause, which guarantees that the objects are properly handled even if an exception is thrown.
Referenced from: http://techiemate.blogspot.com/2009/04/garbage-collection-in-net.html
- Garbage Collection in .NET - How it really works
- How Garbage Collection Really Works
- Java - How Garbage Collection works in Java
- How Java Garbage Collection Works
- How Garbage Collection Really Works 垃圾清理究竟是如何工作的
- w Garbage Collection works in Java
- How Scaling Really Works in Apache HBase
- How Scaling Really Works in Apache HBase
- Java中的垃圾回收是如何工作的?(How Garbage Collection works in Java)
- How Siri really works
- OpenCV+VS, tell you how it really works
- Thinking In Java 之 How a garbage collector works
- think in java笔记:How a garbage collector works
- How Pair Programming Really Works
- Understanding Garbage Collection in the .NET Framework.
- Java垃圾回收机制二 工作原理–How Java Garbage Collection Works?
- 细述 Java垃圾回收机制→How Java Garbage Collection Works?
- How postback works in ASP.NET
- Form:符合在某个条件下记录不能修改,并且背景显示灰色
- do while(0)宏的用法
- 解决并分析mysqld-nt.exe大量占用CPU问题
- 60款精美日历
- 一个没有经验的程序员
- Garbage Collection in .NET - How it really works
- Servlet实现HDFS系统的远程上传
- visual leak detector资料
- Java中日期Date的格式化输出
- linux下.cpio.gz文件的解压
- 刘汝佳黑书 pku等oj题目
- ARM汇编ADR,LDR等伪指令
- TCP长连接VS短连接
- 12步让你的Windows7更好用