I’m currently working on saving the class UsageData to a byte array and storing it on a hardware key. This hardware key has reserved a maximum 256 number of bytes for user data. Currently my filled class with properties has around a size of 640 bytes, already compressed. Now I went over to create a temporarily class UsageDataTemp without any properties/fiels. I run a test to see how large the byte array is without any values, and it already has a size of 220 bytes, only leaving me with 36 more bytes to fill.
[Serializable]
public class UsageDataTemp
{
public byte[] ToByteArray()
{
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
//original serialization is 684 bytes long, compress it with the gzipstream
using (var compressionStream = new GZipStream(stream, CompressionMode.Compress))
{
formatter.Serialize(compressionStream, this);
compressionStream.Flush();
return stream.ToArray();
}
}
}
}
Why is this the case that an empty class without any values already needs 220 bytes for saving itself. And is there a way to compress the byte array down further. Or do I need to start making my own BinaryFormatter.
You don’t tell the serializer/deserializer what type of objects they’re dealing with – you can pass them anything and they “should” work.
That implies that some type information must be stored in the binary stream for it to work out what type of objects are being deserialized. That could easily (I think) account for 220 bytes of overhead.
If every byte counts, then yes, you ought to be writing your own code to transform the data that you’re interested in down to a minimal representation, rather than using a generic mechanism such as the binary serializer.