8 ms·
Garbage Collection vs. malloc/free
- jrochkind1 13y ago> What makes a difference is gc pauses. Imho if you can live without managed memory. You can do this in java as well and eliminate gc pauses. Wait, how can you turn off GC'd managed memory in Java, and do it yourself? I didn't know you could do that in Java?
- pron 13y agoYou don't turn it off, but you can use off-heap memory (with direct ByteBuffers) and manage it yourself. Some Java applications manage some part of their memory off-heap, but this is a bad idea unless the allocated objects have a well-defined lifecycle.
- russfrank 13y agohttp://stackoverflow.com/questions/9323416/using-memory-allocated-by-sun-misc-unsafe-allocatememory-in-native-code http://stackoverflow.com/questions/9323416/using-memory-allo...
- jamii 13y agoYou can even write c-like structs over byte-buffers - https://github.com/ztellman/vertigo https://github.com/ztellman/vertigo
- _pmf_ 13y agoSame for plain Java instead of Clojure: http://javolution.org/target/site/apidocs/javolution/io/Struct.html http://javolution.org/target/site/apidocs/javolution/io/Stru...
- fauigerzigerk 13y agoNow everyone will tell you that you can use ByteBuffer or sun.misc.Unsafe to keep memory off-heap and hence out of sight of the GC. BUT what that means is that you have create your own custom implementations of the data structures you need and you'll have to write your own memory allocator. Unless that memory area holds just a simple data BLOB (like a video file) you're always going to be much more productive implementing the entire thing in C++.
- pjmlp 13y ago> Unless that memory area holds just a simple data BLOB (like a video file) you're always going to be much more productive implementing the entire thing in C++. Or use a GC enabled systems programming language, that provides control over what is allocated via GC or via other means.
- JoshTriplett 13y agoJava's garbage collection is far from the state of the art in GC technology. While there are some non-standard JVMs that have advanced GC (such as IBM's real-time garbage collector), the standard JVM does not. Current-generation garbage collectors have far better performance, and many research GCs support concurrent, pause-free collection. (And that leaves aside the non-performance benefits of a GC, in terms of engineering simplicity and bug elimination.)
- dman 13y agoAny pointers on the state of the art?
- pjmlp 13y agohttp://www.azulsystems.com/technology/c4-garbage-collector http://www.azulsystems.com/technology/c4-garbage-collector
- hga 13y agoWhich I would add is a production garbage collector, an updated version of the Pauseless one that's got its own paper and is covered in the 2nd edition of the book on garbage collection, http://www.amazon.com/The-Garbage-Collection-Handbook-Management/dp/1420082795/ http://www.amazon.com/The-Garbage-Collection-Handbook-Manage... Does require custom hardware or kernel cooperation for speed (e.g. it needs to do batched MMU operations without clearing the TBL on each 2MB page). Looks like it's got a better read barrier than the Pauseless one; that does of course cost extra on stock hardware.
- bnolsen 13y agoIn language garbage collection just makes implementing a compiler that much harder to do and frankly bloats up the runtime. Basic c++ shared pointers take up pretty much 90+% of what people need in GC (non stupid software architecture decisions should make up the remaining 10%). I have seen problems with stalling caused by kernel memory heap defragging on linux resulting in some serious thrashing. What that means is that the issue of heap fragmentation can be a problem regardless of which language used.
- pjmlp 13y agoUnless you happen to have a compacting GC. :)
- gillianseed 13y agoWell since compacting means the expensive task of moving managed memory blocks around to free larger sections that solution certainly comes with a big penalty of it's own. Better to avoid heap fragmentation to such extents by applying a better allocation strategy imo.
- betterunix 13y agoThat is why generational collectors are so good. It is not common to have huge blocks of memory than also happen to be short lived, and if you do have such a design, you can probably refactor it (or just not use garbage collection there, which any production-grade programming environment should allow by some means).
- gsg 13y agoReference counting, even ignoring the additional expense C++ places on std::shared_ptr by requiring it be thread safe, is far too slow for allocation heavy languages. Only "we don't give a damn" implementations (like CPython) use it, because it is simply not competitive.
- pcwalton 13y ago
- pjmlp 13y agoActually the discussion at the newsgroup is more about Java's GC vs malloc/free. In GC enabled systems programming languages like the Oberon family, Modula-3, D, Sing#, one has finer control over the types of available memory. - The usual GC allocated memory with the usual set of possible GC implementations - Stack objects - Global objects statically allocated - Manual memory management inside unsafe blocks via untraced GC references Not all GCs are made alike.
- pron 13y agoReal-time Java (RTSJ) actually has some very cool mechanisms for fine-tuned control over allocations.
- banachtarski 13y agoThe benefit of malloc/free (or new/delete) is that you have control over your own memory layout to handle optimizations like cache alignment, keeping relevant data contiguous, deferring pauses that would introduce latency and such.
- pron 13y agoMemory layout is a separate discussion. The kind of layout optimizations you're describing require work, and if you're willing to put in this work, you can do it in GCed environments as well (like the JVM, with finer-grained control coming in future versions). Otherwise, several/most of the JVM's GCs do a very good job of handling this for you. Objects that were allocated consecutively by the same thread will tend to be kept close together in memory.
- banachtarski 13y agoThat's contiguity but alignment is a different thing. At a certain point though, the two things become one and the same. On the one hand, you could put in layout optimizations in a GC system. On the other hand, you can use things like shared_ptr and effectively write your own GC even. I guess the debate really isn't an A vs B thing. It's just a spectrum and deciding which side of the spectrum you want to start on.
- pron 13y ago> you can use things like shared_ptr and effectively write your own GC even. This is hardly the case, especially on modern multi-core hardware. Writing a GC that works well in a concurrent environment is a huge undertaking. And unless your system is very simple, you'll often be better off with a good general-purpose GC. A shared_ptr introduces a lot of contention.
- ori_b 13y agoThe thing is that most garbage generated is thread local. In fact, depending on the program, it's likely that most of it follows a stack discipline, albeit one that's not delimited by function boundaries. A simple GC (or resettable arena allocator) can make use of this knowledge, and be quite a bit faster in those specific cases. It's hard to beat the man years of work in generic GC that works in all cases, but it's not that hard to beat it in specific situations when the application author knows the details of how the application will do the allocations. 99% of the time, writing your own custom allocation strategies is a waste of time. Sometimes, though, it can be a win, allowing you to allocate and free in a matter of tens of CPU cycles with no GC pauses at all.
- _pmf_ 13y ago> In my experience the biggest difference is stack allocation This is the only voice of reason in this thread full of bullshit by monkeys who have obviously never used C to any extent.
- Pxtl 13y agoI loathe the fact that all intermediate options get left off the table - reference-counting, or single-ownership-with-weak-references, or anything else short of memory-gobbling full GC that breaks your real-time guarantees. And C++'s hideous shared_ptr system isn't a good thing to point to.
- lmm 13y agoIf you have real-time requirements reference counting is worse than GC; it has the same problem of some operations sometimes taking an unexpectedly long amount of time, and the worst-case guarantees are weaker. Single ownership with weak references is effectively what you have in C, though I guess compiler support for distinguishing which references are weak or strong is nice.
- Pxtl 13y agoWeak reference implies a safe error result if you access a weak reference after a "strong" reference has been released, not undefined behavior. Not just a pointer. For refcounting and real-time, just let me leak the cycles. The non-realtime part of reference counting is the cycle detection, let me control the cycle sweeps and everybody's happy.
- RyanZAG 13y ago> let me control the cycle sweeps and everybody's happy. Maybe you are happy, but 99.9% of people would probably be very unhappy with that solution. How would you even go about accomplishing that in a way that wouldn't make you lose your mind? I can only imagine the complexity of having to decide how many cycle sweeps need to be done every time I reduce the reference count on an object/struct. I also think the runtime performance of enforcing that kind of limit would out way any possible benefit.
- millstone 13y agoI think you are referring to deallocating an object graph. Under GC, the cost of deallocating an object graph depends on global state, such as how many unrelated objects are allocated. But in an RC scheme, the cost of deallocating an object graph depends only on that graph itself. This makes RC easier to reason about. The "unexpectedly long" cases can be isolated and optimized separately. Consider a soft real-time environment like a game. Under RC, the time required to render a single frame depends only on the work done in that frame. Get it to 16 ms and you're done. But under GC, deallocation work can accumulate. A frame that would otherwise render in 16 ms may take much longer, due to the laziness of previous frames. That makes it harder to optimize, and indeed you'll often see advice like "frames must do zero GC allocations." Yikes! Things may be different in hard real-time systems, which I know use specialized allocation schemes optimized for worst-case latency instead of bandwidth.
- rayiner 13y agoPeople tend to conflate garbage collection with Java's object model, but the two things are distinct. Most of the time when you're talking about performance limitations in Java, you're really concerned about Java's object model, not the GC. Garbage collection limits your object model, but Java's is much more limited than required for garbage collection. For example, in C++, you might pass small structs on the stack or in registers, while in Java you pass a pointer to a heap allocated object. But there is no reason a garbage collector can't handle value objects allocated on the stack, and indeed the CLR supports that feature. All a garbage collector prevents you from doing is hiding pointers in integers and the like. But that's not as significant a limitation as not being able to say flat arrays of structs, something that is totally compatible with garbage collection.
- pron 13y agoJava value-types (+ arrays) are in the works. The JVM already does what is known as "scalar replacement" (http://www.stefankrause.net/wp/?p=64 http://www.stefankrause.net/wp/?p=64) automatically allocating some non-escaping objects on the stack.
- haberman 13y agoIt's amusing to me that on the same thread GC proponents say that high-performance GC is easy (https://news.ycombinator.com/item?id=6485114 https://news.ycombinator.com/item?id=6485114) and that Java's GC is "far from state of the art" (https://news.ycombinator.com/item?id=6483704 https://news.ycombinator.com/item?id=6483704). And so the perpetual argument goes: GC is superior to manual memory management (the latter of which is "adding needless effort to writing software"), and the performance is just as good, and the cases where performance is not just as good will be fixed Real Soon Now.
- betterunix 13y agoI'd like to point out that I did not say that a high-performance GC is easy. What I actually said is that a simple GC is easier than simple automatic optimizations. A high-performance GC is the same level of difficulty as good automatic optimization.