Binary formatting - does it reduce the size ?

A

Amir Tohidi

Hi

In a nutshell: how can we produce compact persistant versions of .NET objects?

When we first started .NET development five years ago, we opted for TCP/IP
binary formatting for performance and network bandwidth reasons.

We were revisting some of this work and we noticed that our objects, when
saved using a binary formatter aren't shrinking as much as we thought.

Has something changed in .NET? For example, we expected the code below to
generate quite compact binary file, but it doesn't. And it gets worse for our
business objects (they a DataSet as their data carrier) - the binary file
contains loads of (verbose) XML!!!

using System;
using System.Runtime;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace BinaryFormatting_NET1
{
[Serializable]
public class Foo : ISerializable
{
string _s;
int[] _i;

public Foo(string s)
{
_s = s;

_i = new int[1000000];

for (int c = 0; c < 1000000; c++)
_i[c] = c;
}

public void GetObjectData(SerializationInfo si, StreamingContext sc)
{
si.AddValue("_s", _s);
si.AddValue("_i", _i);
}
}


class Class1
{
[STAThread]
static void Main(string[] args)
{
Foo foo = new Foo("hello");


string path =
@"W:\_Common\MiddleTierFramework\MiddleTierFrameWork.Net2.0\Tools\WinSIPPXmlCleanerTests\BinaryFormatting_NET1\foo.bin";
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(path, FileMode.Create);
bf.Serialize(fs, foo);
fs.Close();
}
}
}
 
P

Peter Duniho

[...]
Has something changed in .NET? For example, we expected the code below to
generate quite compact binary file, but it doesn't. And it gets worse
for our
business objects (they a DataSet as their data carrier) - the binary file
contains loads of (verbose) XML!!!

I don't have enough experience with serialization in .NET to answer the
"has something changed" question. But I can tell you that I too have
found that binary representations of my data are surprisingly large, often
comparable to plain text XML representations in size.

I've found that just using plain text XML, but compressing them with the
GzipStream class before writing them out, results in nice small binary
representations of the data.

Pete
 
S

sloan

If your business objects have DataSets in them, then they are not likely to
compact very well.

I would try some tests....with business objects that contain datasets...and
with busines objects WITHOUT datasets.
..

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry
There is some xml serialization tips/examples.

...

I'm surprised binary isn't small/compact from the get-go. However, if I've
ready your post correctly, and you have datasets "under the covers" of your
business objects, then I'm not surprised are your results.
DataSets are universal and sometimes powerful objects. But the flexibilty
and power comes at a price.
Aka, bad serialization is one cost.

...

The best thing is test and compare.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top