473,657 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A bug in .Net Binary Serialization?

Hi all,

I recently came across something really strange and after a couple of days
of debugging, I finally nailed the cause of it. However, I have absolutely no
idea what I am doing wrong or is it just a bug in binary serialization. The
following is a simple example of the code:


using System;
using System.Collecti ons.Generic;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;

namespace ConsoleApplicat ion5
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B(a);
List<CcList = new List<C>();
for (int i = 0; i < 10000; i++)
{
cList.Add(new C("someValue")) ;
}
b.CList = cList;

MemoryStream stream = new MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Se rialize(stream, b);
}
}

[Serializable]
class A
{
private Dictionary<stri ng, string_dic1 = new Dictionary<stri ng,
string>();

public A()
{
_dic1.Add("key1 ", "value1");
_dic1.Add("key2 ", "value2");
}
}

[Serializable]
class B
{
private List<C_cList = new List<C>();
private A _a;

public B(A a)
{
_a = a;
}

public List<CCList
{
get { return _cList; }
set { _cList = value; }
}
}

[Serializable]
class C
{
private Dictionary<stri ng, string_dic2 = new Dictionary<stri ng,
string>();
private string _value;

public C(string value)
{
_value = value;
}
}
}





















If you run the code, you will find that the stream has a length of 4,532,517
bytes. Now, try changing _dic1(Class A) to be a Dictionary<stri ng, object>
and run the code again. Now, the stream length is 462,924 bytes. Why is there
such a big difference just by changing the type? What I noticed also was that
this might be due to the fact that I have another dictionary of the same type
in Class C.

Am I doing something wrong here? If not, is this a bug?

Thanks in advance!!

Jul 2 '08 #1
12 3070
On Tue, 01 Jul 2008 22:50:00 -0700, ztRon
<zt***@discussi ons.microsoft.c omwrote:
[...]
If you run the code, you will find that the stream has a length of
4,532,517
bytes. Now, try changing _dic1(Class A) to be a Dictionary<stri ng,
object>
and run the code again. Now, the stream length is 462,924 bytes. Why is
there
such a big difference just by changing the type? What I noticed also was
that
this might be due to the fact that I have another dictionary of the same
type
in Class C.

Am I doing something wrong here? If not, is this a bug?
I'll vote bug. But I admit, I'm no serialization expert so I might be
missing something.

But I do agree that it seems remarkable that such a simple change in the
type parameter for a Dictionary<TKey , TValueinstance would produce such
a dramatic difference. And I have in fact confirmed the behavior (albeit
with slightly different numbers as the output...but the relative scale is
the same).

It would be interesting to try to serialize to a more readable format and
see what the specific differences are. I don't have the time at the
moment to explore too much, but it's something you might like to try.

Pete
Jul 2 '08 #2
Sorry my post seems to have a huge white space in between. Reposting it below:

Hi all,

I recently came across something really strange and after a couple of days
of debugging, I finally nailed the cause of it. However, I have absolutely no
idea what I am doing wrong or is it just a bug in binary serialization. The
following is a simple example of the code:

using System;
using System.Collecti ons.Generic;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;

namespace ConsoleApplicat ion5
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B(a);
List<CcList = new List<C>();
for (int i = 0; i < 10000; i++)
{
cList.Add(new C("someValue")) ;
}
b.CList = cList;

MemoryStream stream = new MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Se rialize(stream, b);
}
}

[Serializable]
class A
{
private Dictionary<stri ng, string_dic1 = new Dictionary<stri ng,
string>();

public A()
{
_dic1.Add("key1 ", "value1");
_dic1.Add("key2 ", "value2");
}
}

[Serializable]
class B
{
private List<C_cList = new List<C>();
private A _a;

public B(A a)
{
_a = a;
}

public List<CCList
{
get { return _cList; }
set { _cList = value; }
}
}

[Serializable]
class C
{
private Dictionary<stri ng, string_dic2 = new Dictionary<stri ng,
string>();
private string _value;

public C(string value)
{
_value = value;
}
}
}

If you run the code, you will find that the stream has a length of 4,532,517
bytes. Now, try changing _dic1(Class A) to be a Dictionary<stri ng, object>
and run the code again. Now, the stream length is 462,924 bytes. Why is there
such a big difference just by changing the type? What I noticed also was that
this might be due to the fact that I have another dictionary of the same type
in Class C.

Am I doing something wrong here? If not, is this a bug?

Thanks in advance!!
Jul 2 '08 #3
It would be interesting to try to serialize to a more readable format and
see what the specific differences are. I don't have the time at the
moment to explore too much, but it's something you might like to try.
This example was actually derived from a more complex code if that was what
you meant. And in my unit testing of it, I noticed that the size recently
tripled due to the addition of one dictionary even though there is only ever
one instance of it. This was when I started to debug and finally were able to
pinpoint its cause and came out with a simpler example to express this
problem.
Jul 2 '08 #4
On Wed, 02 Jul 2008 00:20:01 -0700, ztRon
<zt***@discussi ons.microsoft.c omwrote:
>It would be interesting to try to serialize to a more readable format
and
see what the specific differences are. I don't have the time at the
moment to explore too much, but it's something you might like to try.

This example was actually derived from a more complex code if that was
what
you meant.
No, it's not. The code you posted was fine. I'm talking about the
resulting data itself. Serialize less, and to a format like SOAP so that
you can take the two alternatives and inspect them as text files
side-by-side. That should give you some clues as to what differences
exist between the two. And that _might_ lead you to some useful
conclusion as to why such a simple change produces such a dramatic
difference.

If you can accomplish that with the output from the BinaryFormatter , more
power to you. :) But I'd go with a text-format serialization. I naïvely
tried to swap in an XmlSerializer for the BinaryFormatter , but of course
it has different requirements from the regular serialization stuff (for
one, it requires everything to be public that's going to be serialized).
I didn't have the time to make the necessary adjustments, but that could
be something you might try, since the output from the XmlSerializer is yet
again much more readable than SOAP.

Pete
Jul 2 '08 #5
The problem with something like the XmlSerializer is that it does not support
serialization of dictionaries.

Does anyone else have any other ideas to this problem?

Thanks.

"Peter Duniho" wrote:
On Wed, 02 Jul 2008 00:20:01 -0700, ztRon
<zt***@discussi ons.microsoft.c omwrote:
It would be interesting to try to serialize to a more readable format
and
see what the specific differences are. I don't have the time at the
moment to explore too much, but it's something you might like to try.
This example was actually derived from a more complex code if that was
what
you meant.

No, it's not. The code you posted was fine. I'm talking about the
resulting data itself. Serialize less, and to a format like SOAP so that
you can take the two alternatives and inspect them as text files
side-by-side. That should give you some clues as to what differences
exist between the two. And that _might_ lead you to some useful
conclusion as to why such a simple change produces such a dramatic
difference.

If you can accomplish that with the output from the BinaryFormatter , more
power to you. :) But I'd go with a text-format serialization. I naïvely
tried to swap in an XmlSerializer for the BinaryFormatter , but of course
it has different requirements from the regular serialization stuff (for
one, it requires everything to be public that's going to be serialized).
I didn't have the time to make the necessary adjustments, but that could
be something you might try, since the output from the XmlSerializer is yet
again much more readable than SOAP.

Pete
Jul 3 '08 #6
On Wed, 02 Jul 2008 18:17:10 -0700, ztRon
<zt***@discussi ons.microsoft.c omwrote:
The problem with something like the XmlSerializer is that it does not
support
serialization of dictionaries.
Not by default, no. You can customize it though. Of course, it's
entirely possible that the problem is related to the default, automatic
serialization of dictionaries, in which case customizing XmlSerializer to
serialize your dictionaries won't help.
Does anyone else have any other ideas to this problem?
Well, like I said, SOAP is also basically text-based and you can use that
as easily as BinaryFormatter .

Pete
Jul 3 '08 #7
But isn't it the same with SOAP? I think SOAP does not support Generics which
thus means that it doesn't support dictionaries?

Well, like I said, SOAP is also basically text-based and you can use that
as easily as BinaryFormatter .

Pete
Jul 3 '08 #8
On Wed, 02 Jul 2008 21:06:00 -0700, ztRon
<zt***@discussi ons.microsoft.c omwrote:
But isn't it the same with SOAP? I think SOAP does not support Generics
which
thus means that it doesn't support dictionaries?
My recollection is that it does. I admit, I haven't tested it recently to
make sure. But I am under the impression that you can just swap in
SoapFormatter where you have BinaryFormatter , and it will "just work".

If I'm wrong, well...it should only take you a few minutes to find out. :)

Pete
Jul 3 '08 #9
I actually did that yesterday using the SOAPFormatter and it did not work,
which was why I thought maybe you meant something else.

"Peter Duniho" wrote:
On Wed, 02 Jul 2008 21:06:00 -0700, ztRon
<zt***@discussi ons.microsoft.c omwrote:
But isn't it the same with SOAP? I think SOAP does not support Generics
which
thus means that it doesn't support dictionaries?

My recollection is that it does. I admit, I haven't tested it recently to
make sure. But I am under the impression that you can just swap in
SoapFormatter where you have BinaryFormatter , and it will "just work".

If I'm wrong, well...it should only take you a few minutes to find out. :)

Pete
Jul 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8702
by: hs | last post by:
Hi I am serializing a dataset using a binary formatter as follows: IFormatter formater = new BinaryFormatter(); formatter.Serialize(stream, ds); // ds=DataSet, stream=MemoryStream .... DataSet ds2 = (DataSet)formatter2.Deserialize(stream2); For the size of my DataSet, its taking 0.8 seconds to serialize and 2.3 seconds to deserialize.
9
6508
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the delta binary is pretty huge in size. I have 1 byte file and 2 bytes file, the delta should be 1 byte but somehow it turns out to be 249 bytes using binary formatter. I guess serialization has some other things added to the delta file.
2
6648
by: Dave Veeneman | last post by:
I'm working on a project where I have to persist data to a file, rather than to a database. Basically, I need to save the state of several classes, each of which will have a couple of dozen instances. I have set it up to use binary serialization to a single file, which works well. However, I have learned that changes to my classes break the old serialized files. While my object model is pretty stable, I am concerned that using binary...
11
11945
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary streams. I ran the test for 50K, 100K, 500K iterations, where each iteration consists of sending an object from a client process to a server process, and the server process sends back an ack. Here are the results: .NET Remoting C++...
7
9551
by: schoenfeld1 | last post by:
I've implemented IPC between two applications using named pipes and binary serialization, but have noticed that the binary formatter is rather slow. It seems that the binary formatter reflects the entire type everytime it is invoked to serialize/deserialize an object of that type. Is there a way to prepare the binary formatter with a pre-defined type, such that it only reflects once but can be re-used to serialize/deserialize objects...
15
2396
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with two int32's as its data members, the binary file of that class must have a size of 8 bytes (i.e. only contains class data members, not methods etc.). Is serialization the answer to the above problem? If I understand correctly, the reason that...
0
1923
by: Vince Filby | last post by:
Hi, We are working with distributing Lucene.net. We have Master Index Server which takes responsibility of distributing the index searching to multiple Index Servers by calling the remote method Search(...) of the Index Server. It is when the Master Server de-serializing the response message from the Index Server, we got the following SerializationException. The Search method is overloaded:
1
11091
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
5559
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as I am trying to read from a binary archive using Boost serialization. I am new to this, and it is possible that I have not understood the usage. In the code below, the string "faultblock" seems to be causing the problem. The code crashes in the ia...
1
5386
by: sandeepbhutani304 | last post by:
have 2 projects communicating each other with .NET remoting. But when I am trying to call these functions I am getting the error: The input stream is not a valid binary format. The starting contents (in bytes) are: 53-79-73-74-65-6D-2E-54-79-70-65-4C-6F-61-64-45-78 ...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.