最近发现存在Dictionary的内存需要过比较久的时间才会释放,尝试以下的方式可以快速实现内存释放
直接上代码
var bytes = GetFileBytes(fileName);
_bytesDict = new Dictionary<int, byte[]>();
_bytesDict.Add(0, bytes);
bytes = null;
_bytesDict[0] = null;
//_bytesDict.Clear();
//_bytesDict = null;
//_bytesDict = new Dictionary<int, byte[]>();
GC.Collect();
首先把变量bytes先置null,然后还要把Dictionary中对应的索引值置null,最后GC回收就可以立刻实现内存释放。
本文介绍了在.NET中,如何通过将`bytes`变量设为null,以及清除Dictionary中对应键值对并调用`GC.Collect()`来快速释放Dictionary占用的内存。
217

被折叠的 条评论
为什么被折叠?



