4 ms·
This is an argument I don't understand. It is exceedingly common for performance sensitive applications in GC environments not to allocate either, because quit
by kasey_junk 11y ago
This is an argument I don't understand. It is exceedingly common for performance sensitive applications in GC environments not to allocate either, because quite simply allocating/deallocating in any environment is expensive.
Why in the world is it fair to compare C/C++ programs that don't allocate to Java/C# ones that do? Another way of saying this is if you are using new ubiquitously then you obviously do not care about performance prima facie.
At the end of the day GC'd versus manual memory debates seem to fall into 2 axis. Complexity vs correctness and total memory available vs concurrent access to said memory.
At this point in time the difference in performance between a C application and a Java one comes down to correctness and access to low level memory layout primitives, not GC vs manual memory.
- okasaki 11y ago> This is an argument I don't understand. It is exceedingly common for performance sensitive applications in GC environments not to allocate either, because quite simply allocating/deallocating in any environment is expensive. Is that possible? I thought all objects are heap allocated in Java?
- eropple 11y agoThey are. So don't reallocate them. Just use mutable objects that can be reset and object pools. (This has its own obvious drawbacks.)
- chrisseaton 11y agoNo this is bad advice in general! It causes the objects to permanently escape, with the knock-on that any objects stored in that object will also escape etc etc, and to move to the old generation. If you need objects, allocate and use them where you need them. This is more likely to allow scalar replacement, or at least more efficient collection while still in the young generation.
- Skinney 11y agoTrue, but object pools enables you to reuse objects, which in turn allows you to avoid or postpone GC (if done correctly).
- chrisseaton 11y agoBut object pools prevent escape analysis and scalar replacement of objects, which wouldn't create garbage in the first place.
- eropple 11y agoIn theory, this sounds true. In practice, it isn't. I've done pooled versus non-pooled tests of objects like Vector3's in libgdx and the difference in memory pressure is fairly huge. The JVM is really, really smart, but this is a well-understood area where you sometimes have to break the rules and do something stupid (and it is stupid) for the sake of perf.
- eropple 11y agoOh--and also consider that some of us (hi) aren't writing for HotSpot, yeah?
- kasey_junk 11y agoParticularly in concurrent contexts escape analysis offers you no help.
- Gankro 11y agoPrimitives aren't allocated. This is why we have `int` and `Integer`; Integer is a boxed int. Java has a fair amount of sugar around doing the change automagically. Also escape analysis is a thing Java can do to avoid heap allocating objects in general: https://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html#escapeAnalysis https://docs.oracle.com/javase/7/docs/technotes/guides/vm/pe...
- david-given 11y agoFun fact! Original Java, before they started adding autoboxing, had precisely one place which did heap allocation: the 'new' keyword (which is why it was a keyword). If you avoided the keyword, you could be sure you weren't using the heap. ...which meant you could run (a limited subset of) Java programs on very, very small devices. 8-bit microcontrollers. Such as Maxim's iButton devices, or smart cards. My memory says that the standard is called kJava, but it seems to be ungoogleable these days, so I could be wrong (and Oracle ate all of Sun's documentation).
- srean 11y agoI think the primary reason is an accident of history. There was a time where Java programmers were encouraged to not even think about allocation deallocation: "dont worry about it, we have it all covered, use it as freely as free love". The result of all that was a lot of bloated poorly performing software that paid no attention to how much allocations they were making. I still see the effects of that propaganda on many junior programmers. GC makes it safe, not free, and that message was never stressed as much it should have been. The other reason is that languages that make you allocate explicitly with some burdensome syntax, that allocation is in your face, you cannot be unaware of it. Whereas in other languages someone may be blissfully unaware and still churn out pieces of usable software. The bite comes much later. On languages that were not designed around garbage collection, it is usually a whole lot harder to avoid allocation.
- pjmlp 11y agoThat and Java not having value types.
- sitkack 11y agoThere were other factors at play during this time which roughly coincided with the Dot Come Boom/Bust. In those days it was ship or die, "money was no object" (unintentional pun) and Sun would be happy to spend people out of their perf well by adding more cpus and ram.