使用 BinaryFormatter 进行序列化 *性能* 问题
背景:
我坚持使用大型对象图,该对象图被序列化为大约 60MB 的文件(并且会随着时间的推移而增长)。问题不在于文件大小,而在于写入和读取时间,在某些机器上长达 4 分钟。
情节:
由于这代表某种内存数据库,我可以延迟加载其中的一些。
Thrill:
如何测量特定对象块的加载时间?我是否记录构造函数调用并从那里开始?还有更好的主意吗?
编辑:
我不想谈论序列化的替代方案,有很多关于该主题的帖子,我宁愿调查为什么它这么慢以及对象图的哪一部分是“切除”和延迟加载的良好候选者。
Background:
I'm stuck with LARGE object graph that gets serialized into some 60MBs of file (and will grow over time). Problem isn't file size but writing and reading times, that go up to 4 minutes on some machines.
Plot:
Since this represents some kind of in-memory database, I can delay-load some of it.
Thrill:
How to measure loading time of specific object chunks? Do I log constructor invocations and go from there? Any better idea?
EDIT:
I would rather not talk about alternatives to serializing, there are lots of posts on that subject, I would rather investigate why is it so slow and what part of the object graph is the good candidate for 'excision' and delay load.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会考虑使用 SQL Server Compact (来自的进程内数据库Microsoft)而不是序列化大型对象图。
我有使用 BinaryFormatter 将大型图形序列化到磁盘的经验。存在与版本控制相关的问题。序列化对象也很难维护和使用。处理此类对象通常需要反序列化到内存中。处理大图是一种消耗资源的方式。
而且Sql Server CE相当轻量,其程序集约为1Mb。它还处理一些多线程问题。
如果您需要序列化只是为了通过网络发送或其他什么,我想 60Mb 是相当大的数组,传输它可能会出现问题。
更新
如果您想使用 BinaryFormatter,我想您可以将图形序列化为较小对象的集合,而不是一个根对象。也许序列化所有对象会更慢,但它会让您仅序列化对象的某些部分。如果您有异构数组(即不同类型的对象序列),我可以假设对象越复杂,其层次结构越深,序列化它所需的时间就越多。您可以测量相同类型的对象集合的序列化时间。您还可以使用一些探查器来序列化整个图,大多数探查器会显示哪个方法需要更多时间来执行。
I would consider using SQL Server Compact (in-proc database from Microsoft) rather then serializing large graph of objects.
I had experience serializing large graph to disk using BinaryFormatter. There were issues related to versioning. Serialized objects are hard to maintain and work with also. And working with such objects generally require to de-serialise into memory. It is resource consuming way to work with large graph.
And Sql Server CE is quite lightweight, its assembly is about 1Mb. It also handles some multi-threading issues.
If you need to serialize just to send over network or whatever, I suppose 60Mb is quite large array, there can be problems transferring it.
Update
If you would like to go with BinaryFormatter, I suppose that you can serialize your graph not as one root object but as collection of smaller objects. Perhaps it would be slower to serialize all the objects but it will let you serialize only some part of objects. If you have heterogeneous array (i.e. sequence of objects of different types) I can suppose that the more complex object is and the deeper its hierarchy the more time it takes to serialize it. You can measure serialization time for a collection of objects of the same type. You also can use some profiler to serialize the whole graph, most profilers show you which method takes more time to execute.
您可以尝试 protobuf.NET,据报告速度更快。
You may try protobuf.NET which has been reported to be faster.