Kus*_*kar 13 .net wpf garbage-collection
当我们分析内存堆时,我们通常会遇到以下4种类型的GC句柄:
弱: - 弱GC句柄不会阻止它对应的实例被垃圾收集.Example, used by the System.WeakReference class instances.
正常: - 正常的GC句柄可防止相应的实例被垃圾回收.Example, used by the instances of strong references.
RefCounted: - 引用计数GC句柄由运行时在内部使用, example, when dealing with COM interfaces.
固定: - 为什么我们需要这种GC手柄?它只是为了避免那个实例在记忆中的运动is there any other notion behind this? I want to know the notion behind Pinned GC handle(with an example).
编辑Itay的答案: - 我有一个非空数组 - DiffCell [] [],它绑定到WPF中的数据网格.当我关闭存在此数据网格的窗口时,在堆上我看到Pinned GC句柄通过object []指向这个空的DiffCell数组(参见快照).I am not using any unsafe code. I am just setting ItemsSource of data grid to null before closing that window. So my question is who does pin this array on heap and why?

我们需要这个,以防我们使用指针.
想象一下,你声明一个指向内存位置的指针而你没有插入
.GC有时会将内存块移动到其他位置,这样你的指针就会失效.
例如:
public unsafe void DoSomething(byte b)
{
byte * bp = &b;
}
Run Code Online (Sandbox Code Playgroud)
这将无法编译,因为您没有修复保存该字节的内存位置.
为了固定它你可以使用:
public unsafe void DoSomething(byte b)
{
fixed(byte * bp = &b)
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3467 次 |
| 最近记录: |