1

Following my recent issues with large object heap fragmentation, I've revisited some of my serializable classes. I frequently do some Xml serialization and serialize/deserialize objects containing large byte arrays.

In general I just use SerializableAttribute and do not bother about serialization details until I really need some special behaviour. Then I switch to IXmlSerializable interface and write my own serialization/deserialization code.

When manually serializing byte arrays to Xml, I prefer XmlWriter.WriteBase64(byte[] buffer, int index, int count) and XmlReader.ReadElementContentAsBase64(byte[] buffer, int index, int count) methods. This allows me to write data in chunks - very convenient.

But what happens when I just use the SerializableAttribute? Does the automatic serialization mechanism inside .NET framework writes data in chunks (like I do manually) or does it just kill large object heap by creating enormously large strings to hold whole of my byte array serialized with base64? Is the serializer efficient, or should I write my own serialization code to make sure the data is written without unnecessary overhead?

4

1 に答える 1

2

Reflectorを使用してSystem.Xml.Serialization.XmlSerializer(およびXmlSerializationWriter、およびSystem.Xml.Base64Encoder)にすばやくアクセスすると、base64バイト配列が変換時にチャンク化されることがわかります。

「シリアライザーは効率的ですか、それともデータが不要なオーバーヘッドなしで書き込まれるように、独自のシリアル化コードを作成する必要がありますか?」

「効率的」はバイナリ条件ではありません。それは連続体です。あなたの本当の質問は「私が必要とするものに対して十分に効率的ですか?」であり、それに対する答えはあなたが必要とするものに依存します。ほとんどの場合、XmlSerializerとカスタムコードの間でいくつかのテストを実行し、最も重要なパフォーマンスの側面に沿ってどちらが優れているかを確認する必要があります。

PSXmlSerializerはSerializableAttributeを使用しません。すべてのパブリック読み取り/書き込みプロパティをシリアル化するだけです。System.Xml.SerializationのXml*AttributesのみがXmlSerializerに影響します(もちろんIXmlSerializableと共に)。

于 2012-04-04T15:02:26.730 に答える