Reputation: 1175
I want to send some objects through sockets from client to server. I can serialize they like object or convert to xml. Which of this methods take less memory?
Upvotes: 1
Views: 337
Reputation: 8633
Naturally serialization takes a lot less memory that converting to XML... think of all those <...>
and </...>
tags! Serialization takes care of all that with numbers, not ASCII characters.
Also, you can serialize to xml! http://x-stream.github.io/
Upvotes: 3
Reputation: 533500
XML vs Java Serialization, one may use more bandwidth, but the main memory used will be your objects. If you are worried about memory used, I would make your object structure more efficient (assuming it is a real issue)
You can stream XML and Java Objects as you serialize/deserialize which is why they shouldn't use much memory.
Obviously, if you build your serialized data before sending it, this will be inefficient.
Upvotes: 1
Reputation: 1300
Converting to XML takes up more space on the client and server than just sending them serialized, since you are basically copying the content into a new variable. Sending them serialized may not use the full capacity of a packet, but you can always just process the first packet and overwrite it with the next to save some space (At least that's how I'm currently doing it).
However, serializing it will probably make the transfer slower, since you have to send multiple packages. On the other hand, if you put everything into one XML, you might run into size restrictions on the packets
(I'm talking about DatagramSocket
and DatagramPacket
here, since these are the ones I use. I dont know how the situation is with other transfer methods).
Upvotes: 1
Reputation: 15219
Serializing them will take A LOT less space. You can also try kryo to get an even better size for your serialized objects. It supports Deflate compression/decompression. Take note however that it's non-standard, so the other side of the socket must use the library as well to de-serialize.
Upvotes: 3