Experiences w/ Garbage Collection and C/C++?
dberger queries: "Java has helped garbage collection enter the mainstream programmer's life - but it's certainly not new or unique to java. There have been (and are) several ways to add garbage collection to C/C++ - the most active seeming to be Hans Boehm's free libgc. I'm curious if any of the Slashdot crowd has used this (or any other) C++ garbage collector in non-trivial commercial applications. If so - what were your experiences? If not, why not? (Before you ask, yes - I know that GC isn't the only difference between C++ and Java, but 'automagic memory management' is certainly part of Java's marketing luster)"
Bjarne Stroustrup, the creator of C++, has this to say on garbage collection:
Clearly, if your code has new operations, delete operations, and pointer arithmetic all over the place, you are going to mess up somewhere and get leaks, stray pointers, etc. This is true independently of how conscientious you are with your allocations: eventually the complexity of the code will overcome the time and effort you can afford. It follows that successful techniques rely on hiding allocation and deallocation inside more manageable types.
He goes on to give detailed examples and recommendations on how to avoid using garbage collection.
void* operator new(size_t n) {
return GC_malloc(n);
}
void operator delete(void* p) {
}
You can also mix collected memory with uncollected memory, but we really don't see the point. This way we can still have descructors which do useful things but the actual memory clean up is left to the garbage collector. Of course, as we write more and more new code we leave our deletes and our destructors out, and eventually we'll go through and remove them all. Until then, we can disable the garbage collector just by #if 0ing these lines out.
How we know is more important than what we know.
The Boehm-Weiser (BW) collector is not as portable as we had hoped. There are a number of platforms we wanted to run on where it just doesn't run at all. Relatively small changes to the target runtime can create a need to port it all over again. OpenBSD, in particular, was an ongoing hassle until we abandoned BW. Hans, I hasten to add, was quite encouraging, but he simply doesn't have time to adequately support the collector.
The BW collector doesn't work in our application. OpenCM has a few very large objects. For reasons we don't really understand, this tends to cause a great deal of garbage retention when running the BW collector. Enough so that the OpenCM server crashed a lot when using it. Please note that this was NOT a bug involving falsely retained pointers, as later experience showed.
Conservative collectors are actually too conservative. If you are willing to make very modest changes in your source code as you design the app, there prove to be very natural places in the code for collection, and the resulting collector is quite efficient.
Independent of the collector, we also hacked together an exceptions package. This was also the right thing to do, but it's easy to trip over it in certain ways. The point of mentioning this is that once you do exceptions the pointer tracking becomes damned near hopeless and you essentially have to go to GC.
I think the way to say this is: exceptions + GC reduces your error handling code by a lot. Instead of three lines of error check on every procedure call, the error checking is confined to logical recovery points in the program, and you don't have to mess around simulating multiple return values in order to return a result code in parallel with the actually intended return value.
To provide malloc pluggability, we implemented an explicit free operation. This lets us interoperate compatibly with other libraries and do leak detection. Turns out to be very handy in lots of ways.
Hybrid storage management works very well. For example, our diff routine explicitly frees some of its local storage (example) [Sorry -- this link will go stale within the next few weeks because the OpenCM web interface will change in a way that makes it obsolete. If the link doesn't work for you, try looking for the same file in .../DEV/opencm/...] This is actually quite wonderful, as it lets us build certain libraries to be GC compatible without being GC dependent. One of the challenges in using a GC'd runtime in a library is compatibility with an enclosing application that doesn't use GC. We haven't tried it yet, but it looks like our gcmalloc code will handle this.
Eventually, we gave up on the BW collector and wrote our own. Our collector is conceptually very similar to the collector that Keith Packard built for Nickle, though we've since built from there. A variant of the Nickle collector is also used as a debugging leak tracer for X11.
The OpenCM GC system is reasonably well standalone. We need to document it, but others might want to look at it when we cut our next release.
On the whole, I'ld say that GC for this app was definitely the right thing to do. Once you get into object caches it becomes very hard to locate all of the objects and decide when to free them. We were able to use a conservative approach with no real hassle, and heap size is fairly well bounded by the assisted GC approach we took.
On the other hand, I would not recommend a pure conservative collector for a pro
Jonathan S. Shapiro (The EROS Guy)
us janitors prefer the term "sanitation engineer".
Do you even lift?
These aren't the 'roids you're looking for.
Garbage collection has costs:
- The obvious: CPU & memory overhead for the checking and tracking. I can't comment on the amount here, but it is a generalized solution, so you forego the optimization opportunities that you'd otherwise have.
- The subtle: Memory allocation can become a major bottleneck in multithreaded systems. Garbage collection has similar issues.
- The irritating: you don't know when your destructors are called.
Another way: Smart Pointers. They're simple wrappers around the types that act like pointers, but they can make sure your objects live as long as you need and no longer. The big trick is knowing which kind of smart pointer you want.
- Reference Counting Smart Pointer (RCSP for short): this type of smart pointer will keep of how many RCSPs are pointing to the same object. It'll delete the object when the last RCSP is destroyed. A good one is the boost shared_ptr. Available for free from www.boost.org. This type is great for general use.
- Owning Smart Pointer (OSP): this type is specialized for those cases when the refcnt is never more than 1. When you assign one OSP (a) to another (b), the new OSP (a) gets ownership of the referred object, and the old one (b) is automatically set to null. When an OSP that isn't set to null is destroyed, it deletes the object it owns. It's great for parameter passing, return values, and objects you want dead at the end of the current scope, even if there's an exception. The STL comes with auto_ptr, which works this way.
You can use an RCSP wherever you can use an OSP, but not the other way around. The STL containers are a great example.
Sure it's not as easy as 'allocate and forget,' but you won't have the (sometimes very costly) expense of full-blown garbage collection.
Also, you can optimize your smart pointers for individual types (through template specialization). A great example is to give the no-longer-needed object back to a pool for later reuse.
This is really a quick, quick overview. For the meat & potatoes, go read Effective STL by Scott Meyers.
I've tried really hard to be fair & polite. There's probably still a bias, but I'm really trying!!
Care about electronic freedom? Consider donating to the EFF!
So please tell me what
typedef vector::const_iterator Iter;
(or rather vector::const_iterator) is supposed to mean. I suppose vector is a templated class, but how does ::const_iterator come up with a type name -- I thought :: either references a static field or a class member function?
And what is the deal with the sort(,) as a free-standing function? Following OO principles, shouldn't the vector object v know how to sort itself with a call to v.sort()? And what the heck is this const_iterator type anyway that you can do ++ and * on it -- looks an awful lot like a pointer -- oops, I forgot, you can overload ++ and * to make "safe" operations on what are really objects look like "dangerous" pointer operations which the C/C++ community is in custom of using.
In principle, all the stuff done in Java and perhaps in scripting languages could all be done elegantly and expressively in C++ if us mere mortals ever figure out how to use the darned thing. But there is a kind of uniformity to Java (all object variables being GC'd heap references, collection and iterator types working with generic Object's that we cast to what the object is and rely on runtime type checking, don't worry-be happy allocation of these objects where we grab towels from the rack and leave them on the bathroom floor for the hotel maid to pick up) that simply feels more comfortable.
C++ is the music of Bach: elegant, mathematical, intricate, and expressive, but most musicians performing in front of audiences don't understand it and it is played as a dull jumble and mishmash and audiences gaze at Bach stuck at the beginning of a recital as a chore to get through. Java is the music of Mozart: simplified, standardized, predictable, and economical, but musicians of this era understand it and play it with gusto, and audiences love it because it sound so happy and makes them feel uplifted.
First i'm not sure this post isn't a troll.. but whatever. While code organization could have a huge impact on the complexity of a program, this is simply not the reason's for most memory leaks in my experience. Most are because of stupid human mistakes. IE creating an array and only deletingthe first node. These are hard to catch because the syntax and everything appears correct at first glance.
Erm then why not do everything in assembly? It's a waste of time to constantly pay attention to details in large projects. Why pay attention to deleting stuff at the proper time, when something like GC can take care of it, at a trivial cost. You increase the speed of development.
This is pure speculation, but my opinion is at first they will be careful about such things, but I think eventually they will realize they can be rather sloppy because the cheap amount they cost, the poeople getting the code, will simply not care so much. I mean if i can have one program for 80k and a similiar one for 10k, but the 10k one has some slightly annoying bugs.. so what?
This is where I think you are trolling. what you've said is like no shit sherlock, but understanding these thigns are not the problem, well in my experience. IMO it is due to human error. It's like asking if a developer has ever made a program that crashed.. mistakes happen. You seem to think that mistakes are for inferior programmers, which is a very arrogant view, and once again, in my experience, is held by extremely ignorant people.
A program variable is either a global variable, a stack variable, a class variable or an instance variable. Global and stack variables are held in lists. Class and instance variables are kept inside objects.
Every class object has a global variable that always refers to it.
Any object that is not, and that can not become referenced (directly or indirectly) by a global or stack program variable is garbage.
Each object has a 'not-garbage' flag.
For each global and stack variable, if the referenced object is not marked not-garbage, mark the referenced object as not-garbage, and recurse for that objects contained variables.
Delete all objects that are not marked not-garbage.
There are a few more twists, like handling return values on the stack, but this algorithm correctly handles self-referencing objects no matter the complexity.
Does everything include nothing?
Erm then why not do everything in assembly? It's a waste of time to constantly pay attention to details in large projects. Why pay attention to deleting stuff at the proper time, when something like GC can take care of it, at a trivial cost. You increase the speed of development.
:-)
All a computer program is *is details*. Every
single non-comment line is a crucial detail.
Using C instead of assembly is because C
takes care of details that I have no need of
taking care of. My overall point here is that
if you don't structure your data structures
in reference to the three types of changes
they incur (addition, modifcation and deletion)
you aren't thinking clearly about you design.
This is where I think you are trolling. what you've said is like no shit sherlock, but understanding these thigns are not the problem, well in my experience. IMO it is due to human error. It's like asking if a developer has ever made a program that crashed.. mistakes happen. You seem to think that mistakes are for inferior programmers, which is a very arrogant view, and once again, in my experience, is held by extremely ignorant people.
To paraphrase a book on Go from a 7-dan,
he said "the difference between the amature
and the professional is that the professional
*never* strays from the fundamentals".
Furthermore, *all* his actions are
manifestations of the fundamentals.
And, *absolutely not*, are mistakes for
inferior programmers. Inferior programmers
don't *catch* their mistakes. Being able
to catch your bugs is what separates the
amature from the professional. Furthermore,
no professional in *any* discipline has
gotten to that level without both making
mistakes and correcting them. The lifelong
amature just doesn't care enough to correct
his mistakes.
the poeople getting the code, will simply not care so much. I mean if i can have one program for 80k and a similiar one for 10k, but the 10k one has some slightly annoying bugs.. so what?
Hmmm. I better not see you bashing Micro$oft for their bug-ridden, insecure os's
Peace & Blessings,
bmac
www.mihr.com: for true peace & happiness
Programms are details, this does not mean programming has to be. Something like GC allows you to abstract a bit more, which increases effeciency which includes speed of development, and lack of bugs, among another things. BUt your overall point, is invalid. If you can't structure your programm halfway decently you shouldn't be doing it, well for money anyhow. Memory leaks that I have seen, are mostly little silly thigns that are inconspicuous. Design and all teh attention to detail will not prevent this from happening. To err is human, right? However when you can automate this, it takes away a possible source of error, which is a good thing, right?
GC is straying away from fundamentals? Fundamentals are a relative thing. fundamental in C is vastly different from LISP or java. C you have to manage memory, it's a fundamental. Java, that is not part of the game. And if you use a GC for C lib, it wouldn't be part of C either.
I aggree, but you can still not catch all your bugs. Debain stable is software from how many years ago? Most people don't even go that far to correct their mistakes. But you seemed to imply that any memory leaks were due to bad programmign design. My point is, good design, while makes these things less frequent does not eliminate them, and that most of the leaks i've seen were not from bad design. They were from human error.
I don't bash them for that, i bash them for being a monopoly and such. Tho i do use that as a point when trying to convince people to switch from windows.
GC is straying away from fundamentals? Fundamentals are a relative thing. fundamental in C is vastly different from LISP or java. C you have to manage memory, it's a fundamental. Java, that is not part of the game. And if you use a GC for C lib, it wouldn't be part of C either.
Yes, GC is just shifting the responsibility,
but the freeing-unused-memory pied-piper must
be paid by something, no matter the language,
no matter the os. And, when it comes right
down to it, we are already developing more
programmers now that *cannot* work on os-level
code (straight C or asm, always) because
they've been indoctrinated into the "just let
another process handle that bit o' trivia".
Also, when it comes time to write an app that
is as fast as possible with the fastest
possible reaction time to the user, a solid
minute of unscheduled, but necessary, GC will
make such an endeavor impossible.
But you seemed to imply that any memory leaks were due to bad programmign design. My point is, good design, while makes these things less frequent does not eliminate them, and that most of the leaks i've seen were not from bad design. They were from human error.
No, good design *could* eliminate bugs, but we
don't yet know how to make such a design. What
I'm referring to is the *process* of creating
software, which involves both good design and
pattern usage inorder to to *allow* a
process that can find the bugs that will
inevitably appear.
And, yes, to err is human. But to seek to
find *all* the errors and fix them, ahhh,
that makes a damn good coder.
Peace & Blessings,
bmac
real programmers *never* put a space in
a filename.
Yes, GC is just shifting the responsibility,
but the freeing-unused-memory pied-piper must
be paid by something, no matter the language,
no matter the os.
Paid for in what sense? I know from experience that I pay in programming time when doing manual memory management. If you mean paid for in CPU time, you should understand that malloc and free take a significant amount of time already. They are not free. GC does not add a great deal to that cost.
And, when it comes right
down to it, we are already developing more
programmers now that *cannot* work on os-level
code (straight C or asm, always) because
they've been indoctrinated into the "just let
another process handle that bit o' trivia".
A master programmer can write low-level and high-level code. If you are writing os-level code, you use os-level tools, and manual memory management is appropriate; if you are writing high-level code, you use high-level tools, and GC is appropriate. I really don't see your point. There are many programmers out there who can only write low-level code (poorly). Too much focus on the low-level means that, for instance, they don't know how to use any datastructures except for arrays.
Also, when it comes time to write an app that
is as fast as possible with the fastest
possible reaction time to the user, a solid
minute of unscheduled, but necessary, GC will
make such an endeavor impossible.
That hasn't been true of garbage collectors for decades. We are not in the 70s any more. Garbage collectors these days are incremental. The biggest optimization gains in software (except in some low-level software) are usually won through algorithmic optimizations rather than low-level optimizations. Garbage collection tends to make such optimizations easier or at least gain you some programming time in which to implement them.
I know it's a very popular opinion here that details must be paid attention too. But when you have a language, tho perhaps a bit slower, allows you to abstract more, You gain a lot more than you lose. And often times you will have a program that is faster than one you would of develoepd with out that abstraction, because You're more worried about solving the problem than all those details. And So waht there's more programmers that don't know these things? Hell, this is good, less compitetion for me
This happens when? you're goign to make leaps and bounds of improvements in hardware and better algorythms compared to optimizing your code. And I believe the military uses mostly Ada for their stuff. And i've never doen ada, but I thought ada had automatic memory management, but I could be wrong on this. BUt still, All these slowness things people love to bring up, are overrated, they are largely irrelevant and trivial. Yes there are cases where they are not. BUt as i said before..
I dunno what you're trying to say.. we will never reach perfection, we will jsut get closer and closer, perhaps we'll reach a point when it's trivial. But we're lightyears from that. So memmory leaks will happen.. if you use GC they won't happen except if there's an error in the GC itself.
There is another indirect cost pointed out by Linus Torvalds in a lengthy post to the gcc mailining list. The executive summary is that (he thinks that) memory that is not to be used anymore should be freed immediately. Otherwise, the data in there will keep lying around in the data cache. Also, he claims that explicit ref-counting gives you advantages for optimization: Assume you have to make some modifications to a data structure, but you don't want other parts of the program to see the modifications. Without ref-counting, you have to copy all the data structure before modifying it. With ref-couting, you can omit the copying if you are the only one with access to the data structure.
And finally, he thinks that GC makes it too easy to write pointer-chasing-heavy code---as that kind of code is bad for cache behaviour all the time.
It is an ongoing discussion whether GC really has that bad effects on performance of GCC. But Linus Torvalds seems to have very good points. (And some of them certainly cannot be taken into account in a "GC cost is less than hand-written memory management"-paper.)
I dunno what you're trying to say.. we will never reach perfection, we will jsut get closer and closer, perhaps we'll reach a point when it's trivial. But we're lightyears from that. So memmory leaks will happen.. if you use GC they won't happen except if there's an error in the GC itself.
"Seek and ye shall find." If you do *not*
seek perfection, you will get just that, and
when you say we you are really saying
I. And to the legions of people who
don't try harder, I say "cheers". Keep up
the mediocre work. In the meantime, what I
seek shall keep getting the same response:
I don't see your point.
At least I can say I tried. Just remember,
it may just be that not "everyone is lightyears
away from that".
Peace & Blessings,
bmac
www.mihr.com: where is that dark matter?
I've used a garbage collection system in a C project before and it works surprisingly well. The problem with GC in C though is that it is possible and legal to,
o allocate memory
o write the pointer to a disk
o lose the pointer in memory
o read the pointer back off the disk,
o make use of the pointer
With all GC strategies I'm aware of, by the time you read the pointer from the disk the memory may well have been freed.
I'm not saying that this style of programming is a generally good idea but it is used in certain, specialised situations and therefore not suitable for a garbage collecting language.
The salient points:
Destructors are not Called
If an object is allocated in collectible memory, then its destructor will not be called when the object is collected. Therefore, destructors are pretty much useless and your code must be designed to work without them.
Actually, if your object derives from class gc_cleanup, then its destructor will be called. However, due to the handling of cleanup functions in the BDW collector, cycles of such objects will never be collected. For this reason, I don't use gc_cleanup much.
Allocating Collectible Memory
By default, C++ allocates objects in the "malloc" heap. The BDW collector maintains a separate heap. In effect, there are four types of memory:
"Scannable" refers to the property that objects in the heap are scanned for pointers. "Collectible" refers to the property that objects in the heap will be deallocated if no further references are found.
These four memory types are an issue when you interact with STL and third-party class libraries. By default, STL uses the malloc heap. If you want, say, a std::vector in collectible memory, then you need to write an allocator to get it. The most recent versions of the collector come with such a beast; the version I started using did not.
Similarly, std::string is reference-counted, and in the malloc heap. Here, rather than using an allocator to force it into the collectible heap, I wrote my own lightweight GCString class, which stores the string as an immutable object, and relies on the collector for cleanup.
Third-party class libraries such as ANTLR may use reference-counted objects; you need to bridge between GC and non-GC applications carefully.
bmac,
have you read Structure And Interpretation Of Computer Programs (SICP) by Abelson, Sussman and Sussman (it's available online, as an MIT *introductory* CS course)?
They use Scheme as their programming language and, in the space of ~600 pages they go from "let's write an expression in Lisp/Scheme" to "let's write a Scheme compiler". Lisp/Scheme uses GC. When they write the compiler, they provide a simple implementation of a GC in the pseudo-assembly they compile to. It's possible to use a language with GC and know *extremely* well what's going on under the hood. And they use a language with a GC to be able to teach sophisticated topics, like logic programming and symbolic differentiation.
I also read Kageyama's book on Go, and ever since I wondered what are the fundamentals of CS. I think many of them are described in SICP, and the most important ones are procedure abstraction and data abstraction.
Also, computers are about automatizing automatizable tasks in a manner that can be "proven" to be "correct". GC is an automatization of memory reclamation which works well (although not "correctly" in some cases because of the environment -- programs with GC have to interact with libs that may fool the GC). It has drawbacks, yes, but then everything else has.
Attention to detail? Yes, but remember that you have to automatize/abstract/detect patterns of data/behaviour, otherwise we'd be stuck with machine code -- no one would have invented a programming language in the first place.
Just my 2 cents.
It turns out, however, that there are natural places to do GC, and a little help from the application can go a very long ways. In the OpenCM collector, we mark procedures that return pointers using a special GC_RETURN macro. This works because at the return from a procedure all of its local variables are known to be unreachable. The only surviving objects are the ones that are reachable from the returned pointer (idea for this is due to Keith Packard).
By using this discipline, we actually blur the distinction between managed and unmanaged collection. The results look very good from a performance perspective.
However, I should acknowledge that this is partly due to the structure of our application. Servers are "in and out". They generate a lot of garbage during a given query and then release essentially all of it. Procedure return is therefore a natural collection point. The same experience might not apply in systems that hold large amounts of memory in the heap during long-running computations, with lots of temporary allocation.
Jonathan S. Shapiro (The EROS Guy)
I pointed out that Stroustrup's example shows the expressive power of C++, but there is a big "huh?" factor of reading the code on account that many of us mere mortals are not rehearsed in the use of templates and STL, and there was something to be said for Java and GC, not just for safety but for simplicity of expression and code reading by maintenance programmers.
Yours is one of three comments to my remarks, answering questions I had raised, disagreeing with some points, agreeing with others, but otherwise engaging in a reasoned discussion of the merits of C++ and its advanced features (templates and STL). But I get moderated down and flagged "troll" -- oh well.
I looked at Stroustrup's two examples. It looks like his first example does not involve freeing any memory at all. Am I right? His second example seems to use auto_ptr to assure that an object is freed when the function where it's allocated returns. Is that all it's doing? I would expect the situations where people get memory leaks to be more complex than auto_ptr could handle.
Anyway, he never mentions garbage collection; just easier "explicit" management. (I put "explicit" in quotes, because malloc/free has to manage free blocks, so it's not as manual a process as you might think. For some applications, Boehm's collector is actually faster.)
How is incrementing/decrementing a reference count "*far* more compute-intensive" than scanning memory?
A few years ago, I used the Boehme GC when writing a pair of compilers (Verilog/VHDL) in C++. I was very happy with the result, since it was rare for GC even to get called at all. It was also surprising how much simpler code gets when you don't have to worry about deleting objects.
I once wrote some classes to work with Memory Mapped Files (under Windows) in an almost transparent manner. It works great for making complex C++ object hierarchies persistant.
ILOG Solver & Scheduler are mainstream commercial thrid party libraries in C++ based on the constraint programming paradigm. One of the major features is ILOG's automatic garbage collection heap, which is automatically deallocates memory (based on assumptions on program flow). To make this efficient, they skip all deallocations (using a longjump, rather than a return).
At first this may look like an elegant way to get rid of complicated memory management & garbage collection without loosing efficiency. However, in my personal experience, it is completely horrible when combined with pats that use the normal system heap. Specifically, when writing your own constraints, goals or deamons, it is practically impossible to use anything but ILOG's solver heap.
I gues this is one of the mayor reasons why they recently made a technology change and launched JSolver, a Java based counterpart.
--
Ninety-Ninety Rule of Project Schedules: The first ninety percent of the task takes ninety percent of the time, and the last ten percent takes the other ninety percent.
if you need garbage collector your code must
be filled with garbage. i, on the other hand,
need only malloc() and free() and still my
code manages to work well without memory leaks
(thanks to valgrind).
The Qt toolkit (on which KDE is based) has a nice garbage collection facility. All of the widgets derived from the base class QWidget take care of deleting child widgets that are also derived from QWidget, including user defined types. This means you can add, remove or move widgets in your user interface without having to worry about the corresponding delete.
Tom.
There's a really good book about everything you ever needed to know about garbage collection. Although most of the book deals with garbage collection techniques in general, it has two complete chapters devoted to implementing and using garbage collectors in C and C++ and which ones you should use depending on your application needs.
...whenever you find yourself writing an overly-complicated means to overcome issues of object/memory 'ownership'.
(Granted, one could say that this would apply to the GC itself, but not necessarily so)
The trick is, memory is a 'resource' and as such is subject to acquisition and release steps in order to maintain it properly. If the notion of ownership of memory is ambiguous, you need to normalize your data somehow so you get back to a 1:n relationship between owners and acquired resources. This happens frequently in situations where objects have an n:n relationship, usually a network of one homogenous type (a network).
The easiest way to achive such a normalization, short of drastically changing your system design or coding practice, is to plug a GC to do object/memory management for you.
As a side note:
Reference counting can help with this style of problem too, but it utterly fails to cope with cycles (mutually pointing objects). See: COM. It can be used *very* effectively if you code with this shortcoming in mind. Some GC's even use ref-counting internally to clear out the bulk of objects, leaving just abandoned cycles to be garbage-collected.
Many people are not aware that GCC itself uses garbage collection as it runs. You can actually select which algorithm gets used at configure time, and tweak the GC parameters during runtime (via a growing set of command-line options that users never think to use).
That aside: I've corresponded with Linus a couple times (on other subjects), and while he is the brilliant guy that /. thinks he is, he is a kernel expert, not a compiler expert. Entirely different problem domain, very differnt approaches to solutions. Not every compiler is alike, not every GC strategy is alike, and most of the GC strategies out there are not appropriate for use within GCC. (Note: within GCC, not by a program compiled with GCC.)
What the GCC maintainers have known for a long time -- due to actual analysis of the compiler, not "this tends to work elsewhere and in other programs" -- is that the current GC strategy is suboptimal. There's even a design for a good replacement. None of the volunteers have had time to write it yet. And on that note, I'll leave you with a quote from Torvalds: "What we need is less people running around and telling everyone else what to do and more people actually writing code."
You cannot apply a technological solution to a sociological problem. (Edwards' Law)
You can get the same effect using Smart Pointers and not give up the control that using a garbage collection system entails. See Boost and Alexandrescu, Andrei. Modern C++ Design. There is also a nice article on CUJ.
Sorry my bullshit sensor overloaded.
If a project is complex enough, you're either going to have to use an existing garbage collector or invent your own. These people who claim that there's no need for GC just haven't had a complex enough project (compilers come to mind).
You can do some things to simplify things: don't share -- make copies instead, use smart pointers (which is really just reference counting GC).
In the end though, you're going to have to start doing pointer scans to get rid of the cycles, and at that point you might as well import an already debugged GC rather than roll your own.
is not great to use in a hybrid C and C++ application. Why? C++'s new operator does not generally take a memory pool as a parameter and the lifetimes of the objects allocated by Apache (on a connection, let's say) do not match the lifetime of the C++ heap allocated objects you may be using. Something could be rigged to have Apache fire callbacks to dispose of C++ resources when a memory pool dies - but this is way too complicated.
If someone was able to get the Apache Portable Runtime working with C++ in a sensible, I tip my hat to you.
Wow! You even realized it was Kageyama's :-) But Kage was the best
book. Man, that is strange, but cool.
BTW, I didn't read the whole book because
I realized that really learning Go was a
full-time occupation and I already have a
few of those
writer though and I got some good info out
of the few tens of pages I did read.
I hate to keep beating a dead horse, but
my whole point here is the programmer
should know exactly what data is no longer
connected and suitably delete it immediately.
And I know this is lost on the small-focused
but this is indicative of our society -- we
just assume someone else will take care of
"it", whatever it is and no matter the the
long-term costs are far greater than the
short-term cost of taking care of the detail
yourself. We package individual pieces of
candy, and I'm nearly the only person who
finds that completely ridiculous. Sure, it
serves its purpose, but with a more broad-
minded approach to life (and respect for
future generations), we could design systems
for food delivery that used 10% of the current
resources. But, like I said, no one wants
to inconvience themselves to improve the
greater efficiency.
And I'm not gonna flame the Scheme/lisp folks,
as a matter of fact I have recently begun
reading said book because there are no more
interesting CS books (at least at B&N, except
for the DirectX & OpenGL books (and I have
no time for all that fun)), so I figured that
I'd trip down the Lisp lane for awhile.
Anyhow, I don't see any os's written in lisp,
and scant few actual programs. (Autocad, one
of the finest pieces of software in the world,
uses lisp for its scripting, but was written
completely in C). Sure, lisp/scheme is neat
for learning and "concepting" and tail-recursion
is neat, but what gets the job done on a
register-based processor is iterative loops,
and Haskell and Lisp just don't map down
better than C or even C++ (without the STL)
will.
Most programmers do not even realize the power
under the hood of even a Pentium II 400 (which
I use). When you write straight C direct to
win32 api calls, the program response is
*immediate*. The program displays *instantly*
and all functions *fly*. And then I load
the SunOne IDE and it takes friggin one
minute. Someone's missing the point here, and
the person who figures out how to write bullet-
proof C code for straight win32 api is going
to rock some dollars.
Peace & Blessings,
bmac
For the fundamentals of life: www.mihr.com
Instead, we developed our own automated object management facility based on reference objects, that is, "smart pointer" objects with these new capabilities: Reference objects in C++ are completely synonymous with object references in Java. Unlike traditional smart pointers, reference objects support inheritance, both single-implementation inheritance and multiple-interface inheritance. These inheritance characteristics also apply to reference objects of arrays. They can also be built to be rigorously threadsafe and secure.
Reference objects have allowed us to port Java source code directly to C++ with virtually no changes. They have also allowed us to take advantage of productive Java language features in new C++ projects without the overhead or installation of any JRE or VM. delete has disappeared from our C++ code entirely. At the same time, our C++ is still characteristically lean and interoperates naturally with other C/C++ libraries and APIs, such as ANSI C++ STL, Win32 API, and MFC.
We have used these concepts to implement the core Java API in C++, for C++, although reference objects themselves are not specific to this API. They are implemented in our low-level Pie Library and usable in any C++ application.
Info and features of NewJ and Pie Library
The NewJ C++ Developer's Guide explains reference objects more fully. It is available as a free download (registration required).
throughout your post I was think, "yes but...", "well put, but..." and then I reached this, which is what I agree with and the only problem I have with GCs.
>The reason is, of course, that you build the pooling based on your knowledge of the actual usage characteristics of the objects; knowledge that no general-purpose memory manager can possibly have.
I find this in general... Garbage Collectors, not unlike VMs, do well when they know their problem domain well. So, it's common to use a Garbage Collector to manage a special heap, say of fixed size objects that are often allocated/deallocated.
The secret to memory management is "don't let things get out of hand", understand how you are using memory. It's not like having to type in a laborious syntax, like say, trying to do OOP in Fortran, which just doesn't have a friendly syntax for that... iow, it's really worthwhile to think about memory management issues, who owns memory, etc, and not just for the free() call, but for the design itself. It pays back a lot to think of these things, and use GC for particular cases that can benefit.
For this reason I don't like GC based languages (or at least, I don't like that aspect of them), while I have nothing against GC in general.
-pyrrho
... well thanks, you made my head explode by saying that!....
Luckilly I don't need my head to type. I can no longer read the articles on slashdot... but that doesn't matter, I can still post.
-pyrrho
You have a nice view as a programmer. Nothing wrong. But as long as you keep the programmer view you wont see that your view regarding the whole topic of "computers, computer science, applying computer sciense to real world problems, variations of solutions depending on programming paradigm, hardware, os, or simply fashion of the current aera" is only a the limted view of a programmer :-) .... you can be assured that memory management and flying functions and instant display of windows ... are completely irrelevant.
:-)
... its a difference wether your loop is written in C, Java or assembler. Of course it is. And if its well written, likely the assembler part is the fastest.
There is far more than only programming in computer sciense.
Supposed you wanted to write a Go program
The "X-price" for writing a Go program that beats a Sho Dan Go player at least *once* is about 4 million dollars. A person playing Go 3 times a week, having a teacher, is likely to reach Sho Dan level in about 3 or 4 years. Just a reference, for how skilled you are as a Sho Dan and how "easy" your program only needs to be
Regarding your assembler loops
However, far more interesting is how often your loop gets called. So your local optimization might be completely useless if your loop is called once a year.
And it also is completely useless if it is called very often but runs only over 3 or 5 elements.
In both cases, *I* as *your* boss want you to spend your time in optimzing the *usage* of said loop, and not the loop.
angel'o'sphere
Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
OK, now I understand. Basically he's saying that if you follow a certain discipline, allocated memory will be freed when the function returns. But the same could be said of explicitly freeing at the end of the function, and having goto's at places where you would otherwise return early. The second example bumps it up exactly one level. However, for objects that get passed around between more than two functions, you still need to keep track of what must be freed.
Regarding Scheme performance, check out how bigloo and Stalin are doing in the Bagley language shootout.
The piper always charges, whether for manual or automatic memory management. The costs have been measured, and for decently large systems, the gains in reliability, maintainablility, and time to market are well worth the additional costs of garbage collection.
As far as your remarks about OS-level code go, people have experimented, successfully, with writing large portions of what might normally be regarded as "the kernel" in a garbage collected language. One example is the SPIN project at the U of Washington during the 1990s. Because they used a safe language (which generally implies garbage collection) they were able to maintain the usual security guarantees while allowing greater freedom in loading code into the kernel. Avoiding the kernel-user boundary at busy interfaces more than made up for the in-the-small overhead of using a safe language. There will always be people capable of writing such code, and compared with the other start-up expenses involved in computing, the costs of motivating those people to be interested in working on the OS will not be excessive. I'm able, and with any luck I'll be alive and mentally sharp (like all my ancestors) for another 40 years.
Another example comes when you do concurrent programming. Reference counting becomes much less attractive when it requires frequent use of bus-locking instructions to keep the counts consistent.
I have also done the experiment of writing code in Java, and then working to insert typed storage pools to avoid activating the GC. The end result does run faster, if you take care to recycle complex data structures without shredding them into parts (by analogy, recycling glass bottles by refilling instead of melting and reblowing), but it does require interface changes, and it does require peculiar distribution of responsibility for reclamation. You might say this is "good design", but it doesn't look good to me -- it looks strictly more complex and more fragile. There is a large quantity of design, but the quality goes down.
And, because this is an economic exercise, it is sometimes worth making the effort to avoid heap allocation, but in practice, like all optimization, this should be done after measurement, not before measurement. Since this will require changes to interfaces, it's not fun, but to simply design the entire system as if it were all one big critical inner loop is wasteful (crazy, actually).
And yes, I AM an Expert. I have done GC research, studied its interactions with optimizing compilers, written interpreters and compilers for garbage-collected languages, written optimizing phases for compilers for those garbage-collected languages, and measured the performance of different garbage collectors and GC-using applications as I optimized them.
Regarding your "solid minute" remark. The last time I played games with rate benchmarking, I saw a rate of 10Mb/second on a 200Mhz Pentium Pro with 66Mhz memory, where the size of the "dense" portion of the live set is what matters (this was with two crude collectors, one of them someone's adaption of the Boehm-Weiser collector). A solid minute of collection would require, on that now-slow processor, 600 Mb of dense live set. This is a worst-case conservative estimate of GC performance -- the collector is crude (not generational), the processor is slow, and I am assuming a large dataset of pointerful objects. In practice, even "crude" copying collections on modern machines run in under a second, because most applications don't have that much live data.
In addition, one can write a provably real-time collector. It's not done often because there are associated overheads (humans are fine with milliseconds of pause, so this would trade off performance losses for no perceived latency gains), but it can be done. Henry Baker wrote a paper on this long ago, and their have been improvements on his work since then (e.g., the "treadmill collector"). Real-time memory management with reference counting always requires careful work, because releas
I've dealt with garbage collection in Java and now in Python. When you get into the mindset of a language that does this natively, I have found that your code naturally flows into that paradigm. I can't imagine trying to use garbage collection in C/C++ -- it just doesn't fit into the scheme of things for me. True, the STL has auto_ptr, and I have used that in the past -- works rather nicely, IMHO -- however the way I learned how to write clean, efficient C code was to make sure you write the code to deallocate memory when you write the code to allocate it, whenever possible. This works for me, mainly 'cause I've done it for so long. Granted, there are times when doing so is difficult -- there are times when you allocate in one function, but don't deallocate until a much later time in another function. I've written a couple of 100K+-line apps that do that (and have to juggle CORBA calls as well) and it is -very- difficult to debug. That's when you wish you had garbage collection. At any rate ... I have found that a number of programmers prefer GC simply because they don't want the hassle of worrying about cleaning up after themselves. For really good programmers ... yeah, it's nice and it allows them to focus more on the task at hand. However, for not-so-good or poor programmers (and there are a number out there) it allows them to write poor code that could allocate memory without considering the consequences -- before I get flamed for this comment, yes I have seen such code, and from some people who were supposedly "really good programmers". When push comes to shove, nothing beats writing good, clean code in the first place, having already designed into the code where memory is allocated and where it is deallocated. Once you've done that, grab the profiler and look for ways to optimize. Chances are, the code will be cleaner and more elegant than if you were to use a GC-type solution. If not, maybe C/C++ isn't the right language for the task. Time to take a look at Python. :)
"computers, computer science, applying computer sciense to real world problems, variations of solutions depending on programming paradigm, hardware, os, or simply fashion of the current aera" is only a the limted view of a programmer :-)
:-)
.... you can be assured that memory management and flying functions and instant display of windows ... are completely irrelevant.
:-)
I actually disagree because we are really
the only people on the planet who deal with
iterations in the millions and billions. Being
able to deal with those numbers (and, more
importantly the Butterfly Effect) gives us a
better understanding of the impact of billions
of human beings being senseless "in the small".
They just don't realize/care that all those
little wrappers add up to a big mess, and,
seeing as I care for the state of the earth
for my children, I'd rather proactively curb
the excess from a systemic point of view
*now* while it's not insoluable. That's why
I turn off the light when I leave the room
and why I free my heap memory as soon as it
isn't needed
Supposed you wanted to write a Go program
Ah, but the performance of the meat of the
algorithm *will* be *extremely* performance
intensive, being the determining factor in
how much look-ahead can be performed (for a
brute-force method) or how many tactics
can be evaluated. GC in that situation is
no different than using GC in your TCP/IP
stack, IMO. You've got to *squeeze* cycles
out of the computer, and GC just doesn't
allow that.
And, in case you don't know, I have no
love for coding the same patterns over and
over again, but I realize that we computer
scientists are about the only craftsman who
make our own tools. So, for me, the question
is "what's the best tool for the job". The
answer to that question probably involves
many different tools, each to a specific realm,
but working closely together. And my version
of this toolset will generated straight machine
code, God Willing
Peace & Blessings,
bmac
www.mihr.com: if you want to know where all
that dark matter & energy are
I don't think you are thinking about what actually happens at CPU level.
If a mutex is active and a thread blocks it will get put into the wait queue and not use CPU. The only ready processes will be new IO, then you are back to the GC code again. The only overhead is the actual scanning on a single CPU system.
You are completely wrong. If a mutex is active and a thread blocks while it waits for a memory allocation it may use a less (but not zero) CPU, but more importantly - its progress has halted. So basically, that database transaction or whatever you have scheduled on this memory blocked thread will wait. Imagine a dozen such threads all blocked waiting for new memory while you have garbage being collected - you have very poor utilization of CPU resources - especially if you have expensive SMP hardware with most of the CPUs sitting IDLE when they could otherwise be doing useful work. We're not talking a few percentage slower here - we're talking several times slower on multi CPU hardware in heavy load.
Do yourself a favor and read about Hoard. Then you'll get a clue about how modern memory management works.
I work in a system that has (mostly soft) real time deadlines. The article states that their
mark-and-sweep algorithm can handle 10MB/s on a 177Mhz machine. It is not unusual for a system to have 500M+ of live data. That's 50 seconds during which the system can do nothing "useful" from an application standpoint, which is 43 seconds beyond our most common deadline. Throwing a faster processor at it doesn't really help all that much.
LOL
Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
I don't think I'm misunderstanding. I understand you have a mechanism that destroys the object a variable is bound to when that variable goes out of scope. That's only useful if you aren't putting the object in a data structure or otherwise planning on using the object outside of the block where that variable is in scope. Basically, it's only useful in trivial cases. I'm not saying it's worthless; I can see it has some advantage over explicitly freeing all the objects. It's just not anything near as powerful as garbage collection. To be fair, Bjarne Stroustrup doesn't present it that way; only aster_ken did.
>Keep up the mediocre work.
Have you ever worked on a large interactive project with dynamic objects shared among multiple users? If so, please tell us how you were able to "know exactly what data is no longer connected and suitably delete it immediately" ?
p.s. Your "Peace & Blessings" signature is bordering on insult when you use it while calling someone an idiot.