13-02 The Basics of Object Lifetime

Allocate a class instance onto the managed heap using the new keyword and forget about it.

在使用new关键字托管堆上分配一个类的实例,并且忘掉他。

Once instantiated, the garbage collector will destroy an object when it is no longer needed. 

一旦实例化,垃圾回收将销毁不在被使用的对象。

garbage collector removes an object from the heap only if it is unreachable by any part of your code base.

Understand, however, that you can’t guarantee that this object will be reclaimed from memory immediately after MakeACar() has completed. 

First, understand that the managed heap is more than just a random chunk of memory accessed by the CLR.

首先,理解托管堆仅仅是可供CLR读取的内存块。

Calculate the total amount of memory required for the object to be allocated

计算需要分配对象所用的总共内存。

Examine the managed heap to ensure that there is indeed enough room to host the object to be allocated. 

检查托管堆,以确保确实有足够的空间能够容纳分配的对象。

When processing the newobj instruction, if the CLR determines that the managed heap does not have sufficient memory to allocate the requested type, it will perform a garbage collection in an attempt to free up memory.

当处理newobj指令,如果CLR决定托管堆没有足够的空间来分配请求类型,它会执行垃圾处理来尝试释放内存空间。

 

posted @ 2015-02-09 16:14  海川0  阅读(53)  评论(0)    收藏  举报