why is the BinaryFormatter output's size so high RRS feed

  • Question

  • Hi there,

    given this example:

    class Program
       void Main(string[] args)
           myFoo = new foo(string.Empty, 
           BinaryFormatter bf = new BinaryFormatter();
           MemoryStream memstream =new MemoryStream();
           bf.Serialize(memstream, myFoo);
           byte[] bytes = memstream.ToArray();
    class foo
       private string foo1;
       private string foo2;
       public foo(string s1, string s2)
            foo1 = s1;
            foo2 = s2;

    Why do I get an Arrylength of 142 bytes although I am serializing a simple class with two empty strings. And wouldn´t it be much more efficient to write my own serializer with Encoding.ASCII.GetBytes , etc...

    Thank you for your answers

    Tuesday, April 7, 2015 10:05 AM


All replies

  • When you create an object/class there is overhead you are not considering. The Net library serializer is optimized (and probably written in C++ without managed overhead) which is going to be more efficient than your own code.


    Tuesday, April 7, 2015 10:41 AM
  • Ok, I think I expressed myself in a wrong way. I didn´t consider my approach to serialise an object to be more efficient than the BinaryFormatter class. But the resulting byte package is much more smaller in my approach in terms of size. So when I use encoding.ASCII I get 1 byte for every character of the strings. Wouldn´t that be better in case I have to send this package over a network for example. Of course I have to make sure that there are some kinds of seperators between the strings and I have to provide a deserialise and serialise methode on both sides. Please correct me if I am wrong.

    But anyways thank you for your answer

    Tuesday, April 7, 2015 3:03 PM
  • Any Serialize method in Net Library creates XML formatted data. You are using serialize. You can send the data as straight binary to reduce the size of the transfer. You can do this either in C++ or use a Marshal method.  See webpage below


    Tuesday, April 7, 2015 4:13 PM
  • No.binarformatter output size is always low than other formatter
    Tuesday, April 7, 2015 5:23 PM