序列化对于对象大小估计是否可靠? [英] Is Serialization reliable for object size estimation?

查看:88
本文介绍了序列化对于对象大小估计是否可靠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用序列化,以便估计用于对象的内存量. 我已经阅读了 this

I use serialization, in order to estimate the amount of memory used for an object. I already read this, and this. So I know that it may be better to use a profiler or sizeof (for value types).

我想知道,序列化对象和内存中对象之间的确切区别是什么?序列化在什么情况下对对象大小估计可靠?

I would like to know, what is the exact difference between the serialized object and the object in memory ? In what measure is serialization reliable for object size estimation ?

我对C#序列化机制特别感兴趣.

I am especially interested in C# serialization mechanisms.

推荐答案

数据的序列化形式与内存中的形式不同;例如,一个集合/词典将涉及多个对象,包括项,数组,哈希桶/索引等-但是原始数据(序列化时)通常 只是数据-所以您序列化时可能会看到较小的音量.

The serialized form of data is not the same as in-memory; for example, a collection/dictionary will involve multiple objects for the items, the arrays, hash-buckets/indexes, etc - but the raw data (when serialized) will typically be just the data - so you might see less volume when serialized.

同样,像BinaryFormatter之类的东西必须包含很多(详细的)类型元数据-但是在对象中,对象句柄中只有一个(简洁的)类型句柄-因此您可能会看到更多序列化数据中的数据.同样,序列化程序(除非手动对其进行优化)需要标记各个字段-但在内存中,这隐含在与对象地址的偏移量中.

Equally, things like BinaryFormatter have to include a lot of (verbose) type metadata - but in the objects it just has a (terse) type handle in the object handle - so you might see more data in the serialized data. Likewise, the serializer (unless it is manually optimized) needs to tokenize the individual fields - but in memory this is implicit in the offset from the objects address.

因此您可能会从序列化中获得 a 号,但不是相同号.

So you might get a number from serialization, but it is not the same number.

要准确了解对象图的大小是很棘手的. SOS可能会有所帮助;否则,创建它们的整体负载并进行划分.粗暴的,但它也许行得通.

To get an accurate idea of the size of an object graph is tricky. SOS might help; otherwise, create a whole shed-load of them and divide. Crude, but it might just work.

这篇关于序列化对于对象大小估计是否可靠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆