Why such a widespread .net dictionary

I am serializing a general dictionary in VB.net, and I am very surprised that it is about 1.3kb with one element. Am I doing something wrong, or is there something else that I should do? I have a large number of dictionaries, and he kills me to send them on all wires. The code I'm using 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 
+7
generics serialization
source share
2 answers

The default serialization for the dictionary should include type information for the type of dictionary used by the comparator and for the types of each element (both key and value), since they can be generally subtypes. These overheads must be added for each dictionary. If you print the data as a string, you can see that there are many fully qualified types, occupying many bytes:

\ 0 \ 0 \ 0 \ 0 ???? \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0? System.Collections.Generic.Dictionary 2[[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.GenericEqualityComparer 1 [[System.Int32, mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]] \ b? System.Collections.Generic.KeyValuePair 2[[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.GenericEqualityComparer 1 [[ 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.KeyValuePair 2[[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.KeyValuePair 2 [[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 want to use a custom format for serialization, as well as a standard format that is a bit lighter, like JSON .

+7
source share

In setting up a dictionary for serialization, a lot of overhead is used (obviously, about 1.3kb;)). However, you will find that the size does not increase much as more elements are added to the collection if you use primitive types for keys and values.

This overhead is mostly one-time, before the cost starts - therefore, as soon as you receive a series of Dictionary classes, the contained elements will not add the same size.

+4
source share

All Articles