In my current project (OpenGL Voxel Engine) I have a serious issue when generating models. I have a very object oriented structure, meaning that even single parameters of my vertices are Object
s. This way I am creating about 75000 Object
s for 750 voxels in about 5 seconds. Is Java really this slow when allocating new Object
s or do I miss a big failure somewhere in my code?
-
It varies among the type of Object(s). – Elliott Frisch Jan 10 '14 at 22:01
-
because you are using the object type? – esmoreno Jan 10 '14 at 22:02
-
A good JVM is probably faster overall at creating objects than a C++ system. It's just that there are so many objects used in Java (especially when the app is poorly designed). – Hot Licks Jan 10 '14 at 22:03
-
It also depends on how long the objects might live, if you're creating a bunch of short lived objects, the GC might kick in, reducing the performance. I've implemented Object pooling before, win allowed to go from 500 moving object so to 4500 all using basic Swing graphics... – MadProgrammer Jan 10 '14 at 22:04
-
There isn't really much code to show. Just some constructors and a array filled with Object for the model. – th3falc0n Jan 10 '14 at 22:04
-
@MadProgrammer - Of course, generally speaking, a GC scheme is faster than a malloc/free scheme or a reference counter or whatever, for a given number/size of objects allocated/freed. – Hot Licks Jan 10 '14 at 22:05
-
Are you creating 75000 or 750*75000 objects? – John Koerner Jan 10 '14 at 22:09
-
Without seeing code, we can't give you more help. If you could at least post the constructors (and methods they call) that could clear things up. Particularly if there are some nested loops, which can kill performance. – Reinstate Monica -- notmaynard Jan 10 '14 at 22:10
-
I am creating 75000 objects. I am currently trying to extract the code so you can reconstruct the whole scheme. Improved it a bit by changing from Lists to arrays in the vertices. Now creating 200000 objects in 5 seconds. – th3falc0n Jan 10 '14 at 22:12
-
3OK i just localized that I am retesselating my VBOs for every Vertex I am adding. Problem solved. – th3falc0n Jan 10 '14 at 22:14
6 Answers
Very big question. Generally speaking, it depends from the object class definition and by the amount of work required to construct object.
Some issue:
- avoid finalize method,
- tune memory and GC in order to avoid excessive GC activity,
- avoid big work during constructor,
- do not use syncronization call during object construction,
- use Weak references
these issues solved my problem.
See also http://oreilly.com/catalog/javapt/chapter/ch04.html
Finally let me suggest you the (deprecated) Object Pool pattern or reuse objects.
Concluding, no, generally speaking, java object creation is not slow

- 7,469
- 2
- 48
- 70
-
1In this particular, I believe the problem is point 3: lot of work during construction. I would just change the last sentence in `generally speaking, Java is really fast in creating object`. I found some benchmarks few months ago to prove this, I should look for them again – ThanksForAllTheFish Jan 10 '14 at 22:21
-
-
1to start: http://onlinevillage.blogspot.ch/2011/03/is-javascript-is-faster-than-c.html on a side note, this is also interesting (does not show result, but it goes deep into java performance monitoring): http://www.ibm.com/developerworks/library/j-5things8/ – ThanksForAllTheFish Jan 10 '14 at 22:37
Of course it isn't. The following code allocates 10 million objects and stores them in an array. On my 5 year old notebook, it completes in 1.4 seconds.
public class Test {
public static void main(String[] args) {
Object[] o = new Object[10_000_000];
long start = System.nanoTime();
for (int i = 0; i < o.length; i++) {
o[i] = new Object();
}
long end = System.nanoTime();
System.out.println(Arrays.hashCode(o));
System.out.println(new BigDecimal(end - start).movePointLeft(9));
}
}
... and that's even though this benchmark is quite naive in that it doesn't trigger just in time compilation of the code under test before starting the timer.

- 68,356
- 14
- 108
- 175
Simply creating 75,000 objects should not take 5 seconds. Take a look at the work your constructor is doing. What else are you doing during this time besides creating the objects? Have you tried timing the code to pinpoint where delays occur?

- 265,237
- 58
- 395
- 493
Objects will be slower than primitives, and they will also consume considerably more memory - so it's possible you are going overboard on them. It's hard to say without seeing more details.
75000 objects will not take a long time to create though, try this:
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 0;i<75000;i++){
numbers.add(i); // Note this will autobox creating an Integer object after the first 128
}
System.out.println(numbers.size());
}
http://www.tryjava8.com/app/snippets/52d070b1e4b004716da5cb4f
Total time taken less than a second.
When I put the number up to 7,500,000 it finally took a second...

- 40,716
- 16
- 83
- 128
-
1Thanks for the interesting website, but I think I just crashed the servers o.O – th3falc0n Jan 10 '14 at 22:18
The new operator in java is very fast compared to the common approach in languages without automatic memory management (e.g. the new operator is usually faster than the malloc-command in C because it does not need a system call).
Although the new operator can still be a bottleneck, it is certainly not the problem in your case. Creating 75K objects should be WAY faster than 5 seconds.

- 38,872
- 15
- 103
- 162
I have the same issue with creating new objects.
My object in constructor allocate single three dimensional array 64x64x64 and no more. FPS fell down to quarter of a value.
I solve this issue with reusing old object and reset it's state (BTW this method reallocate this array without lost performance).
If I move allocation array into separate method and call it after creating the object, speed does not increase to acceptable value.
This object I created is in Main game loop.

- 1