Take the 2-minute tour ×



share|improve this question
"Java object creation is the most expensive operation you could perform". Mind sharing the source of that claim? –  Songo May 22 '12 at 9:23
And - most expensive operation as compared to what? Compared to calculating pi to 900 digits or compared to incrementing an int? –  jasonk May 22 '12 at 9:55
Most expensive (primitive) operation is actually creating a very large array... –  Mehrdad May 23 '12 at 6:20
This is one of the most funny things I've ever heard about programming .) –  Mert May 24 '12 at 8:58
show 1 more comment


11 Answers

up vote 210 down vote accepted

Your colleague has no idea what they are talking about. Your most expensive operation would be listening to them, they wasted your time mis-directing you to information at least a decade out of date as well as you having to spend time posting here and researching the Internet for the truth.

Hopefully they are just ignorantly regurgitating something they heard or read from a decade ago and don't know any better. I would take anything else they say as suspect as well, this should be a well known fallacy by anyone that keeps up to date either way.









Current GC algorithms are actually optimized for creating many many small objects that are short lived, that is basically the 99% heuristic for Java objects in every program. Object Pooling will actually make the JVM preform worse in most cases. The only Objects that need pooling today are Objects that refer to finite resources that are external to the JVM; Sockets, Files, Database Connections, etc.



share|improve this answer
+1: "Your most expensive operation would be listening to them ...". Best I've heard for some time. –  Cwan May 22 '12 at 8:16
@DeadMG, even with the cumulative GC overhead, Java can be faster than C++ (e.g., due to the heap compaction, minimising cache misses for certain data structures). –  SK-logic May 22 '12 at 8:43
@DeadMG, what are you talking about? There are no extra indirections. And no, you cannot match the proper GC performance with the custom allocators, pools and all that. Please do not come back to this topic unless you're able to prove that you can infer a region-based memory management scheme for any possible program in, say, simple lambda calculus. You cannot compactify manually a custom binary tree coming from runtime-dependant data. –  SK-logic May 22 '12 at 8:52
@Matsemann that is exactly the false information that the OP is referring to. It hasn't been accurate for a decade. Object polling is relevant for resources external to the JVM and that are finite, like DB Connections/Network Sockets. All that old mumbo jumbo about Object pooling in any other context is voodoo now. –  Jarrod Roberson May 22 '12 at 18:06
show 12 more comments



share|improve this answer
+1 Even though the garbage collector "just works", every Java programmer should learn about the generational garbage collector. –  benzado May 22 '12 at 13:16
An the next step for escape analysis is to "allocate" memory for small object purely in registers (for example a Point object could fit in 2 general purpose registers). –  Joachim Sauer May 23 '12 at 5:34
@Joachim Sauer: That's kind of what is done in the newer implementations of the HotSpot VM. It's called scalar replacement. –  someguy May 24 '12 at 15:02
@someguy: I've read about it some time ago as the next thing, but didn't follow up to check if it's already done. It's excellent news to hear that we already have this. –  Joachim Sauer May 24 '12 at 19:31
show 1 more comment




share|improve this answer
Wow! 20+ MB to process 50 rows of data? Sounds crazy. In any case, are those objects long-lived? Because that's the case that would matter for GC. If on the other hand you're merely discussing memory requirements, that's unrelated to garbage collection or object creation efficiency... –  Andres F. May 22 '12 at 23:52
Objects are short-lived (less than 0.5sec in the general case). That volume of garbage still affects performance. –  jasonk May 23 '12 at 23:17
add comment



share|improve this answer
Its worth noting that escape analysis will allow short lived (bounded life) objects to be stored on the stack, these objects can be deallocated for free ibm.com/developerworks/java/library/j-jtp09275/index.html –  Richard Tingle Jun 27 '13 at 10:36
@Richard: great point - escape analysis provides some very good optimisations. You have to be careful about relying on it though: it isn't guaranteed to happen in all Java implementations and in all circumstances. So you often need to benchmark to be sure. –  mikera Jun 27 '13 at 11:43
add comment






share|improve this answer
Most modern JVM's support better garbage collection algorithms than "stop the world". The amortized time spent in garbage collection is frequently less than what is spent explicitly calling malloc/free. Also does C++ memory management run in a separate thread? –  user1249 May 22 '12 at 6:59
I don't think any of the standard garbage collectors shipped with the JVM support pauseless GC yet - the pause is shorter but not gone. Some are working on pauseless GC: infoq.com/news/2010/09/bigmemory en.wikipedia.org/wiki/Azul_Systems Also here's a StackOverlow thread that mentions the deallocation method I referred to: stackoverflow.com/questions/390102/… –  greg May 22 '12 at 12:05
Depends on which JVM you use. We use a non-x86 IBM JVM for production, which can do pauseless gc. –  user1249 May 22 '12 at 12:07
The newer Java versions from Oracle can do escape analysis, which means that objects that don't escape a method are allocated on the stack, which makes cleaning them up free - they are automatically deallocated when the method returns. –  Jesper May 22 '12 at 14:37
@Mehrdad - it runs in parallel without pausing the JVM. It does - however - stop the world if the gc cannot keep up (a gc is requested before the previous gc has finished). This usually happens only when swapping badly. –  user1249 May 23 '12 at 7:48
show 9 more comments


Other than that, the above answers hold true.

share|improve this answer
Read the question again. It does not specify JVM anywhere including the tags. It only mentions Java. –  Peter Kelly May 22 '12 at 15:08
add comment



share|improve this answer
add comment

the GC is tuned for many short lived object

that said if you can trivially reduce object allocation you should


String str = "";
    str+= appendingString;


you can easily rewrite this into:

StringBuilder strB = new StringBuilder();
String str = strB.toString();



share|improve this answer
the biggest advantage in this StringBuilder approach is the pre-size the StringBuilder so that it never has to reallocate the underlying array with the StringBuilder(int) constructor. This makes it a single allocation, rather than a 1+N allocations. –  Jarrod Roberson May 22 '12 at 18:11
@JarrodRoberson StringBuilder will at least double the current capacity or in other words the capacity will grow exponentially for only log(n) allocations –  ratchet freak May 22 '12 at 19:09
add comment



share|improve this answer
If the additional cost is moved to the 'if (firstTime)', than recreating class in a loop is still more expensive than reusing the class. –  Alex May 24 '12 at 14:19
add comment



share|improve this answer
It would be very constructive if people put comment also while down voting any answer. After all we are all here to share knowledge, and simply passing judgment without proper reason is not going to serve any purpose. –  AKS Aug 2 '13 at 11:33
Stack exchange should have some mechanism to notify users who down vote any answer, when comment is posted on that answer. –  AKS Aug 2 '13 at 16:46
add comment




share|improve this answer
add comment

protected by Yannis Rizos Aug 2 '13 at 5:39

