c++/cli 为我们提供了垃圾收集器并为我们进行内存管理,那么为什么我们对对象使用删除运算符呢?
我的问题是为什么我们对对象使用删除运算符? .net 不为我们提供它吗?
此外,我编写了一个带有两个按钮的小程序。在第一个按钮中我创建了一个对象,在第二个按钮中我删除了它。我在程序运行时查看Windows进程资源管理器,但是当我单击第二个按钮时,内存并没有减少。没看懂流程?
My question is why we use delete operators for our objects?
Does not .net supply it for us?
Additionally, I write a small program with two buttons. In the first button I created an object and in the second button I delete it. I Look over the windows process explorer while the program is running, but when I clicked the second button the memory did not decrease. I did not understand the process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++/CLI 中的删除运算符与 C++ 中的运算符执行的操作不同。事实上,内存是由垃圾收集器管理的,你不能显式释放它。但对象可以使用内存之外的其他资源,最常见的是操作系统句柄或指向非托管堆内存的指针。垃圾收集器也会释放它们,但这需要时间。如果资源很昂贵,您将希望在不再使用它时释放它,而不是等待终结器处理它。这在 .NET 中称为处置。
在按钮对象上使用delete会立即产生结果:按钮消失。按钮控件的窗口是这些操作系统资源之一。这通常不会减少内存使用量。句柄计数减少。
The delete operator in C++/CLI doesn't do the same thing as the operator in C++. Indeed, memory is managed by the garbage collector, you cannot release it explicitly. But objects can use additional resources beyond memory, most typically they are operating system handles or pointers to unmanaged heap memory. The garbage collector will release those too but that takes time. If the resource is expensive you'll want to release it when you have no use for it anymore instead of waiting for the finalizer to take care of it. This is called disposing in .NET.
Using delete on a button object has an immediate result, the button disappears. The window for the button control is one of those operating system resources. This will in general not reduce the memory usage. The handle count goes down.
垃圾收集器有自己的内存回收算法,称为非确定性处置,您可以使其立即由 GC.Collect() 调用
The garbage collector has it's own algorithm for reclaiming the memory, it's called nondeterministic disposal, you can make it to be called immediately by
GC.Collect()