简体   繁体   English

C#/ .NET:字典内存使用情况

[英]C#/.NET: Dictionary memory usage

I've got a generic Dictionary that holds a bunch of data. 我有一个包含大量数据的通用词典。 I'd like to start removing items (I know how to determine which ones) when the size of the Dictionary starts taking up too much memory. 当Dictionary的大小开始占用太多内存时,我想开始删除项目(我知道如何确定哪些项目)。

What's the best way for my program to monitor the RAM used by this Dictionary? 我的程序监视此词典使用的RAM的最佳方法是什么?

Each item in the Dictionary is a variable size. 字典中的每个项目都是可变大小。

Work out the size of each item and multiply by the Dictionary.Count 计算出每个项目的大小,并乘以Dictionary.Count

One way is shown here : 这里显示一种方法:

An approximate way to determine the size of object usage in memory can be done by checking the total memory before and after the creation of the object. 确定内存中对象使用大小的近似方法可以通过在创建对象之前和之后检查总内存来完成。 In the following example it is assumed you have a class named foo. 在以下示例中,假设您有一个名为foo的类。

 long StopBytes = 0; foo myFoo; long StartBytes = System.GC.GetTotalMemory(true); myFoo = new foo(); StopBytes = System.GC.GetTotalMemory(true); GC.KeepAlive(myFoo); // This ensures a reference to object keeps object in memory MessageBox.Show("Size is " + ((long)(StopBytes - StartBytes)).ToString()); 

C# has a 'sizeof' operator that works like it does in C/C++, however it returns the size that a field of that type will be. C#有一个'sizeof'运算符,它的工作方式与在C / C ++中的运行方式相同,但它返回的是该类型字段的大小。 Thus for reference types (class, not struct), it will always return the size of a pointer (4 on 32 bit systems). 因此,对于引用类型(类,而不是struct),它将始终返回指针的大小(32位系统上的4)。 Use System.Runtime.InteropServices.Marshal.SizeOf() instead. 请改用System.Runtime.InteropServices.Marshal.SizeOf()

I don't believe there is any API which will give you the true size of an object, but it can be determined by measuring the ammount of memory used before and after the object is created; 我不相信有任何API可以提供对象的真实大小,但可以通过测量创建对象之前和之后使用的内存量来确定; however, that is not really thread safe. 但是,这不是真正的线程安全。

You could use System.Runtime.InteropServices.Marshal.SizeOf() even though this will not give you an accurate size it will give you an accurate comparison point so that you can say something like: if the size of the object increases by X% then remove Y number of elements. 您可以使用System.Runtime.InteropServices.Marshal.SizeOf(),即使这不会给您一个准确的大小,它会为您提供一个准确的比较点,以便您可以这样说:如果对象的大小增加X%然后删除Y个元素。

Also, you could try to keep a running total but if items in the collection are editable you will need to make sure that every addition, removal, and edit is included in the calculation. 此外,您可以尝试保持运行总计,但如果集合中的项目是可编辑的,则需要确保计算中包含每个添加,删除和编辑。 You are still left with the problem of properly measuring the items being added and removed and as stated above I don't believe there is an API that will do this accurately. 您仍然存在正确测量正在添加和删除的项目的问题,如上所述,我不相信有一个API可以准确地执行此操作。

当您向Dictionary添加内容时,计算项目添加的大小并将每个添加项目的总和保持为单个int值,当您从字典中删除项目时减少此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM