.net获取任务管理器内存_.NET中的内存管理

.net获取任务管理器内存

A basic question.. “What is the Garbage Collector?”

一个基本的问题。“什么是垃圾收集器?”

The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.”

给出的通常答案是:“垃圾收集器是CLR运行的后台线程,用于释放程序不再使用的对象所占用的内存空间。”

I wondered why the interviewers changed their facial expressions when I finish saying this answer... :)

我想知道为什么当我说完这个答案时,面试官会改变他们的面部表情... :)

It’s because they expect something different from us..

这是因为他们期望与我们有所不同。

For answering this, we should start with the Managed Heap concept in .Net. It’s not new to us; we all know the basics.

为了回答这个问题,我们应该从.Net中的托管堆概念开始。 对我们来说这并不新鲜; 我们都知道基础知识。

First of all, when we start running our program, the .Net runtime reserves a continuous region of space in memory. This space will be free at this moment. This space region is what we call the Managed Heap.

首先,当我们开始运行程序时,.Net运行时将在内存中保留连续的空间区域。 此空间此时将是免费的。 这个空间区域就是我们所说的托管堆。

The Heap also has a pointer, whose job is to point to the region in the heap, where the next object is to be allocated. At this point, since the whole Heap is free, this pointer will point to the base address of the Heap.

堆还具有一个指针,该指针的工作是指向堆中要分配下一个对象的区域。 此时,由于整个堆是空闲的,因此该指针将指向堆的基地址。

Now, consider our program contains a line of code: Object tempObject = new Object();

现在,考虑我们的程序包含一行代码:Object tempObject = new Object();

The “new” operator which we saw in the line has an important role in memory allocation.

我们在该行中看到的“ new”运算符在内存分配中起着重要作用。

The “new” operator first makes sure that the bytes required for allocating the object is free in the Heap. If the required bytes are present, it allocates the memory.

首先,“ new”运算符确保分配对象所需的字节在堆中是空闲的。 如果存在所需的字节,它将分配内存。

But still, “tempObject” has not become an object yet. This is because .NET runtime has not got any information regarding this entity (“tempObject” is not yet considered an object)

但是,“ tempObject”尚未成为对象。 这是因为.NET运行时尚未获得有关此实体的任何信息(“ tempObject”尚未视为对象)

.NET runtime doesn’t know even what type the entity is. By reading the line of code, we now know that the TYPE of tempObject’s is System.Object. But .NET runtime doesn’t know it right now.

.NET运行时甚至不知道实体是什么类型。 通过阅读代码行,我们现在知道tempObject的类型为System.Object。 但是.NET运行时现在不知道。

All that .NET runtime knows now is, tempObject is some kind of thing for which memory has been allocated.

.NET运行时现在所知道的就是,tempObject是某种已为其分配内存的东西。

Then the “new” operator calls the constructor of the class Object, since tempObject is of type Object. This is what we call Object Initialization.

然后,由于tempObject属于Object类型,因此“ new”运算符将调用Object类的构造函数。 这就是我们所谓的对象初始化。

Now since the “new” operator has finished running the constructor of the class Object, it has got all the required information. This is the point, where .NET runtime realizes that tempObject is an object of type System.Object. So now tempObject is not an entity anymore, it’s a fully fledged .NET object.

现在,由于“ new”运算符完成了Object类的构造函数的运行,因此它已经获得了所有必需的信息。 这就是.NET运行时意识到tempObject是System.Object类型的对象的关键所在。 所以现在tempObject不再是一个实体,它是一个完整的.NET对象。

Now the final task of “new” operator is, it returns the address of this object to the .NET runtime. So now .NET runtime has got the address of this tempObject also, by which it can access this object.

现在,“新”运算符的最后一项任务是,它将此对象的地址返回到.NET运行时。 因此,.NET运行时现在也有了该tempObject的地址,通过它可以访问此对象。

Now as tempObject has been stored in the Heap, the Heap pointer (which we mentioned above) will now point to the next free space in the Heap.

现在,由于tempObject已存储在Heap中,因此,Heap指针(我们在上面已提到)现在将指向Heap中的下一个可用空间。

In the same manner, all the objects are stored.

以相同的方式,存储所有对象。

This whole process which is designed by Microsoft, was built under one assumption. The assumption was “Memory space is infinite”.

由Microsoft设计的整个过程是在一个假设下构建的。 假设是“内存空间是无限的”。

As we read the process above, .NET runtime keeps on allocating memory on the Heap infinitely, no matter how many objects are present in the program.

当我们阅读以上过程时,.NET运行时将继续在Heap上无限分配内存,无论程序中存在多少个对象。

As a solution to this, they developed a mechanism which frees up memory where possible, so that their assumption remains true and the system continues to work. This mechanism was named Garbage Collector.

作为对此的解决方案,他们开发了一种在可能的情况下释放内存的机制,从而使他们的假设保持正确,并且系统可以继续工作。 该机制被称为垃圾收集器。

翻译自: https://www.experts-exchange.com/articles/4202/Memory-Management-in-Net.html

.net获取任务管理器内存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值