JSON serialization libraries comparison. GSON vs Jackson
I heard someone saying serialize into strings same as serializing into stream or writer. That doesn’t make sense to me and I wanted to see if there is a difference or not.
I generated a List of random 100 objects and let's see which method performs better speed and memory efficiency. I prepared 4 different test cases and watched memory consumption on the JVisualVM. I run each test 100000 times in a single-threaded same environment. You can find my source code here
- GSON generate into a string
- GSON generates into a StreamWriter.
- Jackson generates into a string.
- Jackson generates into an OutputStream.
And here is the result:
Total Time: 20.784.280.357 nanoseconds.
Memory consumption is around 160MB a lot of GCs. You can see it on the JVisualVM screenshot.
Total Time: 31.990.323.311 nanoseconds.
Memory consumption is around 155MB. I expected this test to perform much better. But It didn’t. It is worse than generate to string. What a weird day.
Total Time: 6.260.122.921 nanoseconds.
Memory consumption is around 155MB. Only a few GC. Evidently, this is performing better than GSON all the way.
Total Time: 7.331.952.325 nanoseconds.
Memory consumption is around 12MB. Almost no GC required. Only few small GC. Maybe a little slower than the previous one. But it is okey, This is what I expected from serialization libraries.
The clear winner is the Jackson2Stream test. For future applications, I will use this all the way. But I’m still wondered why the GSON2Writer test this bad. Maybe GSON developers forget something.