• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object serialization and pooling

 
Uri Cohen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I use a simple object pool for object reuse.
The objects in this pool are later serializaed to network clients.
The problem is that the default serialization mechanism will not serialize an object twice, even if it changed between two subseqent calls to ObjectOutputStream.writeObject(). This is because it uses the object's identity hash code to determine wheteher or not the entire object or just a "reference" to the previously serializaed object should be written to the stream.
So effectively, you cannot risk serializing pooled instances. JDK 1.4 partially solves this with the method ObjectOutputStream.writeUnshared(), but this only works for the base level object, and not to "transitively referenced sub-objects in the object graph to be serialized", as the docs say. Anyone has any ideas how to solve this (without twicking each object seprately using Externalizable or other mechanisms)?

Thanks,

Uri
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps making an object pool is a case of premature optimization:
Java Hot Spot FAQ

Should I pool objects to help GC?
No!
Pooling objects will cause them to live longer than necessary. The garbage collection methods will be much more efficient if you let it do the memory management. We strongly advise taking out object pools.

Object pools make sense where the cost of creating an object exceeds the cost of keeping the object around. High-cost objects, like database connections, make sense to pool. It sounds like you are pooling data objects, as you are attempting to send them across a network. Since they are rather inexpensive to create and initalize and the HotSpot VM is tuned to handle large numbers of short-lived objects, I'd say get rid of the pool.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!