Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Remoting / July 2003

Tip: Looking for answers? Try searching our database.

Size used to serialize ints using BinaryFormatter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hugh Robinson - 30 Jul 2003 20:36 GMT
I am new to remoting and looking at what format to use to serialize my
objects. I thought I'd do some tests and I can't understand why I am
getting the results that I am. I am simply serializing a single int
and it seems to take 54 bytes, not 4 as I would expect. If I were
serializing a non-primitive object, I could understand that there's
some header info etc., but a primitive?

Also if I serialize 10 ints, it scales linearly and takes 540 bytes.

Maybe I am using the MemoryStream class wrong?
Thanks, Hugh

Here's the simple code:

try {
  IFormatter formatter = new BinaryFormatter();
  MemoryStream stream = new MemoryStream();

  int nTest=7;
  formatter.Serialize(stream, nTest);

  // this prints 54, not 4 as I would expect!
  Debug.WriteLine(stream.Length);

  // aby.Length is also 54...
  byte[] aby = stream.ToArray();
} catch (Exception ex) {
  Debug.WriteLine(ex.Message);
}
Rob Teixeira [MVP] - 30 Jul 2003 23:49 GMT
There are indeed serialization headers here. For one, the serialization
formatter needs to know what Type the data is (and by default also captures
things like the complete qualified type including version). Otherwise you
have a bunch of bits and no way to determine how to deserialize it.

-Rob [MVP]

> I am new to remoting and looking at what format to use to serialize my
> objects. I thought I'd do some tests and I can't understand why I am
[quoted text clipped - 25 lines]
>    Debug.WriteLine(ex.Message);
> }
Stanislaw Szczepaniak - 31 Jul 2003 10:35 GMT
If you replace the BinaryFormatter by the SoapFormatter
you are able to read what's needed to store (type
information etc.)
I think the overhead doen't matter for larger objects and
does it make sence to serialize a primitive?

And with serialization you have to differnet
possibilities, Xml Serialization and Runtime Serialization.
With the Xml Serialization you do not have to store the
type information and e.g.
public class Person
{
  public string Name;
  public int Age;
}

with

Person p = new Person();
p.Name = "Tom";
p.Age = 17;

would be serialized simply as

<Person>
  <Name>Tom</Name>
  <Age>17</age>
</Person>

using Xml Serialization.

Regards,
Stan
Hugh Robinson - 31 Jul 2003 17:07 GMT
Thanks for the posts.

The reason I was looking at serializing primitive ints is that I was
thinking of writing my own serialization routines for each class that
I want to serialize and I was curious as to how much space data really
takes on the wire. I was thinking that if I implemented my own
serialization then I only need 4 bytes for an int, because I know the
format of the data on the wire (I realize that doing that could lead
to some versioning issues).

I hadn't got as far as looking at SerializationInfo and ISerializable
- I suppose I should look at those next - actually, I think I was
thinking about it from a Java point of view of implementing
serialization (where there isn't a serializationinfo object). Clearly
I need to do more research

Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.