java.beans.XMLEncoder Considered Harmful

To all java engineers out there:

Before you even think about using java.beans.XMLEncoder and it’s sibling, XMLDecoder, make sure you verify two things:

Are you OK with your application running single-threaded? XMLEncoding hits a static synchronized helper method. Expect to see blocked threads.

Are you OK with forcing your JVM to process an obscene amount of object garbage? A 1 hour stress test generated more than 1.2 Tb of object garbage. While the stress test was running, the JVM was between 50 and 80% devoted to garbage collection.

After switching to xstream, the GC problems are gone, there aren’t any blocked threads, my whites are whiter, my coat is a whole lot more glossy, and cherubs are flying by with banners that say “Huzzah.”

Related posts:

  1. Java for Mac OS X 10.5 Update 1 adds support for Java SE 6
    Java for Mac OS X 10.5 Update 1 adds support for Java SE 6 Update: Apple’s java6 port can’t run IDEA… Back to java5. :-\......
  2. java rake db:migrate implementations
    I was surprised that there were so many libraries that do the same thing — manage change in  your database schema. Here’s what I’ve found so far: Liquibase (migrations are......
  3. Set up JAVA_HOME to track Java Preferences.app on Mac OS X
    Mac OS X’s Java Preferences.app has a pane for switching between versions of the JDK, but I just found out from a coworker (thanks, Mike!) that you can make your......
  4. TestNG, test groups, IDEA, and @BeforeMethod
    Say you have public class SomeTestNG { @BeforeClass public void setup() { … } @Test public void test1() { … } @Test public void test2() { … } } You’ll......

  • Sathiya

    Matthew,

    When I was considering how to encode Java Objects to XML for my PDF report generation architecture, I faced the same problem. Decided to go with XStream instead of java.beans.XMLEncoder.

    The primary reason was the way the java.beans.XMLEncoder encodes Java onjects into XML it kinda sucks. Xstream was a perfect it. Xstream is the way to go. I faced no performance issue whatsoever with XStream with day one.

    By the way if you are wondering who I am… http://www.linkedin.com/in/sathiyas

  • http://goldchest.sourceforge.net Dan Howard

    Except Xstream has no allowances for versioning. If a class in the object graph ever changes XStream blows up. There’s sort of solution with XMT but it’s not very well supported.

  • http://matthew.mceachen.us matthew

    I would highly recommend NOT using serialization for something that needs versioning–it’s way too easy to break.

    If you need versioning, use a storage structure that’s explicitly designed for long-term storage — something like Google’s Protocol Buffers.