• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to serialize java object

 
Cheenath Ajay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I am using Java default Object serialization to transfer object from one JVM to the other. The object transfer is through tcp socket.

As the object size is really huge (there are multiple data-structures like ArrayList, HashMap and so on ), the object serialization, object transfer through tcp socket and de-serialization taking relatively huge time.

To solve this issue I tried using Xstream (XML based serialization, which is supposed to be better performer than Java serialization). Unfortunately since the object is huge in size the XML generation, size of XML, de-serialization taking more time than Java serialization.

Can anyone suggest me a better serialization methodology in this scenario?
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised to hear that XML-based serialization would perform better than plain Java serialization. Do you have a cite for that? It might be that it could be a better performer if you used it to limit the amount of data you were sending. That's the direction you should be looking. Serialization is great for small data objects like DTOs or other Java Beans, but serializing a huge data structure is not ideal. You spend a lot of your time recording and restoring all the object relationships as well as the raw data. You need to examine what you really need to communicate to your remote machine, and what's the fastest way to do that.
 
Cheenath Ajay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg Charles,

I 100% agree with you on this, XML conversion during serialization and de-serialization is time consuming process.
I was under a process of evaluating available techniques to serialize and de-serialize huge data in the best manner.

A XML framework may a decent representation of data, but may not be the best performer during data transfer.

Now I can leave the users of my framework to externalize object that needs to transferred to remote machine in an optimized manner (as they know the object better), or create a generic wrapper code which can serialize any object to minimize the object size in a faster manner.

There are couple of ideas in my mind.
1. Use NIO for data transfer
2. Transfer the object in compressed format
3. Evaluate Google Protocol Buffer

Do you have any suggestion?

Thanks in advance,
-Ajay






 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know exactly what you're sending across the wire, but I'd suggest just serializing the data part if you can, and avoid sending a overly complicated object tree. Also take a close look about what you actually serialize. I remember once using serialization to send client state over to a server, and it was going really slow. On analysis, we found that one of the client classes had a reference back to a UI component ... which had a reference to its parent ... which had a reference to its parent plus all its children ... and so on. Yes, we were unintentionally serializing the entire user interface along with the data.

Assuming you're not as dumb as we were back then, then compressing the data might help. Also it's not hard. Just stick a ZipOutputStream somewhere in your stack. You know, something like:



Actually, I can't remember if that's enough for ZipOutputStream, or if you have to prime it with a ZipEntry first. It's been awhile since I've used it, but I know it was pretty easy. Anyway, that might help if your network is especially slow and/or your data compresses nicely. It's worth trying anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!