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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/850833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:09:20  来源:igfitidea点击:

C#/.NET: Dictionary memory usage

c#.netdictionary

提问by core

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.

我有一个包含一堆数据的通用字典。当字典的大小开始占用太多内存时,我想开始删除项目(我知道如何确定哪些项目)。

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.

字典中的每个项目的大小都是可变的。

回答by Mitch Wheat

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.

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());

可以通过检查对象创建前后的总内存来确定内存中对象使用大小的大致方法。在以下示例中,假设您有一个名为 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. Thus for reference types (class, not struct), it will always return the size of a pointer (4 on 32 bit systems). Use System.Runtime.InteropServices.Marshal.SizeOf()instead.

C# 有一个 'sizeof' 运算符,它的工作方式与 C/C++ 中的一样,但是它返回该类型字段的大小。因此,对于引用类型(类,而不是结构),它将始终返回指针的大小(在 32 位系统上为 4)。使用System.Runtime.InteropServices.Marshal.SizeOf()来代替。

回答by dimarzionist

When you adding something to Dictionary, calculate size of item adding and keep sum of every added item as single int value, when you're removing item from dictionary decrement this value.

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

回答by jellomonkey

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; however, that is not really thread safe.

我不相信有任何 API 可以为您提供对象的真实大小,但可以通过测量创建对象前后使用的内存量来确定;然而,这并不是真正的线程安全。

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 可以准确地做到这一点。