views:

125

answers:

2

I am serializing a generic dictionary in VB.net and I am very surprised that it is about 1.3kb with a single item. Am I doing something wrong, or is there something else I should be doing? I have a large number of dictionaries and it is killing me to send them all across the wire. The code I use for serialization is

    Dim dictionary As New Dictionary(Of Integer, Integer)
    Dim stream As New MemoryStream
    Dim bformatter As New BinaryFormatter()

    dictionary.Add(1, 1)

    bformatter.Serialize(stream, dictionary)

    Dim len As Long = stream.Length
+3  A: 

There is a lot of overhead involved in setting up the dictionary for serialization (obviously, roughly 1.3kb ;) ). However, you'll find that the size doesn't grow dramatically as more elements are added to your collection, provided you're using primitive types for keys and values.

This overhead is mainly a one time, up front cost - so once you get the Dictionary class serialized, the contained members don't add as much size.

Reed Copsey
+5  A: 

The default serialization for a dictionary has to include type information for the type of the dictionary, the comparer used, and for the types of each of the items (both key and value) because they might in general be subtypes. This overhead has to be added for each dictionary. If you print the data as a string you can see that there are a lot of fully qualified types taking up a lot of bytes:

\0\0\0\0????\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.Dictionary2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0\aVersion\bComparer\bHashSize\rKeyValuePairs\0\0\b?System.Collections.Generic.GenericEqualityComparer1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\b?System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][]\0\0\0\t\0\0\0\0\0\0\t\0\0\0\0\0\0?System.Collections.Generic.GenericEqualityComparer1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0\0\a\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]?????System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0keyvalue\0\0\b\b\0\0\0\0\v

You might prefer to use a custom format for serialization, or else a standard format that is slightly lighter such as JSON.

Mark Byers