Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between serialization and normal object storage?

Serialization is the process of converting an object stored in memory into a stream of bytes to be transferred over a network, stored in a DB, etc.

But isn't the object already stored in memory as bits and bytes? Why do we need another process to convert the object stored as bytes into another byte representation? Can't we just transmit the object directly over the network?

I think I may be missing something in the way the objects are stored in memory, or the way the object fields are accessed.

Can someone please help me in clearing up this confusion?

like image 809
chaudharyp Avatar asked Dec 19 '22 20:12

chaudharyp


2 Answers

Different systems don't store things in memory in the same way. The obvious example is endianness.

Serialization defines a way by which systems using different in-memory representations can communicate.

Another important fact is that the requirements on in-memory and serialized data may be different: when in-memory, fast read (and maybe write) access is desirable; when serialized, small size is desirable. It is easier to create two different formats to fit these two use cases than it is to create one format which is good for both.

An example which springs to mind is LinkedHashMap: this basically stores two versions of the mapping when in memory (one to capture insertion order; one as a traditional hash map). However, you don't need both of these representations to reconstruct the same map from a serialized form: you only need the insertion order of key/value pairs. As such, the serialized form does not store the same data as the in-memory form.

like image 105
Andy Turner Avatar answered Dec 21 '22 09:12

Andy Turner


Serialization turns the pre-existing bytes from the memory into a universal form.

This is done because different systems allocate memory in different ways. Thus, we cannot ensure that the object can be saved directly from the memory on one machine and then be loaded back in properly into another, different machine.

Mabe you can find more information on this page of Oracle docs.

like image 20
fill͡pant͡ Avatar answered Dec 21 '22 10:12

fill͡pant͡