MemoryMappedFile 或序列化,非常大对象的速度

发布于 2025-01-07 04:27:59 字数 735 浏览 5 评论 0原文

我有一个项目,它有一个相当复杂的嵌套(向上和向下调用引用对象的引用)对象(类),存储在字典中,例如:

public static Dictionary<string, Object1> DObject { get; set; }

Object1 是一个复杂的类。它不像“地址簿”或“人员信息”类。类本身具有另一个对象的数组、字典。这些对象引用它上面的类,等等。

因为 DObject 的大小在内存中可以是 1GB 以上,所以我将使用 BinaryFormatter 将其序列化为文件。因为我需要加载这个对象,所以我正在考虑使用MemoryMappedFile。新的键和值可能会添加到字典中。对象中可能包含更多数据(添加/更新)等。MMF 是否会更改大小?如何访问内存映射文件中DObject中的某个键?是否有像内存中的哈希表这样的搜索机制,以便我可以像字典一样找到某个键​​并获取其值?这个 MMF 是如何工作的呢?

我的理想想法是。磁盘上有一个大文件 (2GB+)。我在磁盘上快速更新字典中的对象,就像在磁盘上保存内存一样。一切都很快。键值对一路查找。我查找、编辑值、保存等...我必须随时快速访问这个 2GB+ 的对象。如果 WCF 服务器重新启动,我需要快速访问这个 2GB 以上大小的对象。这就是为什么我在想 1) 序列化 2)。从/向 MMF 加载和读取。我现在主要关心的是速度。换句话说,我不能每次调试项目时都从头开始重新加载这 2GB 数据(这需要很长时间)。

关于我应该如何处理这种情况的任何建议、想法和想法。

I have a project that has a fairly complicated, nested (references calling the referenced objects up and down) object (class) stored in a dictionary like:

public static Dictionary<string, Object1> DObject { get; set; }

Object1 is a complicated class. It is not like a "address book" or "person info" class. The class itself has arrays, dictionaries of another object. Those object references class above it, etc.

Because the size of the DObject can be 1GB+ in memory, I'm going to serialize it to a file using BinaryFormatter. Because I need to load this object, I was thinking of using MemoryMappedFile. New keys and values might be added to the dictionary. Objects might have more data in them (added/updated), etc. Does the MMF change size? How do I access a certain key in the DObject in the memory mapped file? Is there a searching mechanism like a hashtable in memory so I can find a certain key like a dictionary and get its value? How is this MMF work anyway?

My ideal thought is. Have a large file on disk (2GB+). I update the object in the dictionary fast on the disk like having a memory saved on the disk. Everything is fast. Key-value look up all the way. I look up, edit the value, save, etc... I would have to have quick access to this 2GB+ object any time I want. If the WCF server restarts, I would need quick access to this 2GB+ size object. That's why I was thinking 1) serialization 2). load and read from/to MMF. My main concern is speed at this moment. I can't be reloading this 2GB data from scratch everytime I debug the project in other words (it takes long time).

Any suggestions, ideas and thoughts on how I should handle this situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2025-01-14 04:27:59

您不能将 DObject 本身保留在内存映射文件中。 .NET 对象往往会一直移动,您不能强迫它们保留在内存中的一个位置(您可以固定它们,但您确实不想这样做)。您可以序列化该对象并将其写入文件,但这意味着每次写入超过 1GB 的数据。

为什么不采用标准方法,将对象“序列化”到一组数据库表中?

You can't keep DObject itself in a memory mapped file. .NET objects tend to move around all the time, and you can't force them to stay in one place in memory (you could pin them, but you really don't want to). You can serialize the object and write it to file, but that would mean writing more than 1GB of data each time.

Why not go the standard way, and 'serialize' your object to a set of database tables?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文