Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
kohlerm
Employee
Employee
0 Kudos
Hi all,

Brian Goetz claims that object allocation in Java is very fast and that therefore it doesn't make sense to preallocate or pool objects in Java. Brian is very well known in the java community for his contributions to the concurrency framework and therefore a lot of people will just believe what he has to say.


But in this case I have to disagree with him and I'm not alone.

Yes, object allocation in Java is very cheap, yes it's even cheaper than a malloc in C, which comes at no surprise because already years ago, a "new" in Smalltalk was faster than a malloc. But the time for allocation objects is not the complete cost,that you have to take into account.

Todays Garbage Collection algorithms are suprisingly efficent and often in a single thread test program you may never see huge delays because of GC activities. But in on a multithreaded server with hundreds of threads running, and several VM's running on more than one machine in a clustered enviroment, the situation is a different one.

First, with enough threads allocating objects at a high rate, you will run into the situation that more and more objects will be promoted to the so called old space, which will trigger a Full GC, which usually will stop the world for several seconds. These full GC's may stop activity on the whole cluster in case someone does a broadcast to all cluster nodes and waits for the result. We also know that a Full GC can have a non linear effect on performance, because of Implications of Java Garbage Collection for Multi-User Applications

You might also want to pool objects such as Strings, because you have to optimize memory consumption. If you read a String from the DB more than once, but this String is really some constant or unique identifier, you really don't want to hold duplicates of this String in memory.

So please be careful, if "someone" tells you that you don't really need to take care much about how many objects you allocate in your Java application.

Update: Kirk has posted a very well written comment on his blog here. I fully agree with his points.


Regards,
Markus
2 Comments