digitalmars.D - About RAII and GC...?
- Nick Sabalausky (1/1) Jul 30 2007 Maybe I'm misunderstanding things, but if you use RAII wherever appropri...
- Robert Fraser (2/3) Jul 30 2007 If the only resource you're acquiring at construction is memory (90% of ...
- Nick Sabalausky (19/27) Jul 30 2007 That's not exactly what I'm asking. I'll explain:
- Witold Baryluk (6/14) Jul 31 2007 You are probably right. If absolutely all variables (with references typ...
- Jarrett Billingsley (12/15) Jul 30 2007 Go ahead and program your entire program without persisting any state
- Daniel Keep (4/22) Jul 30 2007 Scoped member variables; second half of my old proposal :P
- Nick Sabalausky (3/18) Jul 30 2007 Ahh, I see.
- Sean Kelly (5/11) Jul 30 2007 I think Walter intends to use "scope" for that. ie.
Maybe I'm misunderstanding things, but if you use RAII wherever appropriate, wouldn't that eliminate the need for garbage collection? Is there something I'm missing here?
Jul 30 2007
Nick Sabalausky Wrote:Maybe I'm misunderstanding things, but if you use RAII wherever appropriate, wouldn't that eliminate the need for garbage collection? Is there something I'm missing here?If the only resource you're acquiring at construction is memory (90% of cases), you can just use the GC instead. Also, the GC isn't necessarily incompatible with RAII -- if there's some resource you don't need to release as soon as possible, the GC will run the destructor on its own.
Jul 30 2007
"Robert Fraser" <fraserofthenight gmail.com> wrote in message news:f8m283$2q9b$1 digitalmars.com...Nick Sabalausky Wrote:That's not exactly what I'm asking. I'll explain: As we know, the reason for having garbage collection is that when using malloc/new there's a general tendency to forget the occasional free/delete (or end up with an exectution path that accidentially omits it), thus causing memory leaks. GC, of course, fixes that by saying "Don't worry about freeing, I'll clean up after you". Now, maybe I'm misunderstanding RAII, but as I understand it, RAII guarantees that allocated memory (or whatever resource) gets freed automatically whenever the owning object goes out of scope. So that seems to indicate to me that RAII is not only more general than GC (ie, can work for more than just memory) but also lacks any overhead of having a GC (aside from the usual overhead of freeing a resource). Thus, RAII can effectively replace the use of a GC. Of course, with RAII you still need to manually release resources (right?), but you only need to do it in the destructor (right?). And this is vastly easier to not screw up than having "new"s and "delete"s all over the place. So, does my analysis of this make sense, or am I off-base?Maybe I'm misunderstanding things, but if you use RAII wherever appropriate, wouldn't that eliminate the need for garbage collection? Is there something I'm missing here?If the only resource you're acquiring at construction is memory (90% of cases), you can just use the GC instead. Also, the GC isn't necessarily incompatible with RAII -- if there's some resource you don't need to release as soon as possible, the GC will run the destructor on its own.
Jul 30 2007
Nick Sabalausky wrote:Now, maybe I'm misunderstanding RAII, but as I understand it, RAII guarantees that allocated memory (or whatever resource) gets freed automatically whenever the owning object goes out of scope. So that seems to indicate to me that RAII is not only more general than GC (ie, can work for more than just memory) but also lacks any overhead of having a GC (aside from the usual overhead of freeing a resource). Thus, RAII can effectively replace the use of a GC.You are probably right. If absolutely all variables (with references types) is declared with scope, then they will be destroyed explicitly and GC will have probably nothing to do. But you will need to write allocators for this types of variables, because compiler will use GC allocator (but will free memory explicitly like with delete) if not.
Jul 31 2007
"Nick Sabalausky" <a a.a> wrote in message news:f8m0fq$2ogm$1 digitalmars.com...Maybe I'm misunderstanding things, but if you use RAII wherever appropriate, wouldn't that eliminate the need for garbage collection? Is there something I'm missing here?Go ahead and program your entire program without persisting any state outside the program execution stack. Let us know how it goes ;) Basically RAII only works for local function variables. If you want the lifetime of an object to be controlled by something other than the execution time of a function, that's what the GC is for. Although I wonder how much the GC could be helped by means of a new kind of object lifetime control, where the lifetime of one object is controlled by the lifetime of another. Collecting the "parent" object would cause the "child" object to be collected, and the child couldn't be collectable until the parent object was.
Jul 30 2007
Jarrett Billingsley wrote:"Nick Sabalausky" <a a.a> wrote in message news:f8m0fq$2ogm$1 digitalmars.com...Scoped member variables; second half of my old proposal :P http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=38329 -- DanielMaybe I'm misunderstanding things, but if you use RAII wherever appropriate, wouldn't that eliminate the need for garbage collection? Is there something I'm missing here?Go ahead and program your entire program without persisting any state outside the program execution stack. Let us know how it goes ;) Basically RAII only works for local function variables. If you want the lifetime of an object to be controlled by something other than the execution time of a function, that's what the GC is for. Although I wonder how much the GC could be helped by means of a new kind of object lifetime control, where the lifetime of one object is controlled by the lifetime of another. Collecting the "parent" object would cause the "child" object to be collected, and the child couldn't be collectable until the parent object was.
Jul 30 2007
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message news:f8m50f$2soh$1 digitalmars.com..."Nick Sabalausky" <a a.a> wrote in message news:f8m0fq$2ogm$1 digitalmars.com...Ahh, I see.Maybe I'm misunderstanding things, but if you use RAII wherever appropriate, wouldn't that eliminate the need for garbage collection? Is there something I'm missing here?Go ahead and program your entire program without persisting any state outside the program execution stack. Let us know how it goes ;) Basically RAII only works for local function variables. If you want the lifetime of an object to be controlled by something other than the execution time of a function, that's what the GC is for. Although I wonder how much the GC could be helped by means of a new kind of object lifetime control, where the lifetime of one object is controlled by the lifetime of another. Collecting the "parent" object would cause the "child" object to be collected, and the child couldn't be collectable until the parent object was.
Jul 30 2007
Jarrett Billingsley wrote:Although I wonder how much the GC could be helped by means of a new kind of object lifetime control, where the lifetime of one object is controlled by the lifetime of another. Collecting the "parent" object would cause the "child" object to be collected, and the child couldn't be collectable until the parent object was.I think Walter intends to use "scope" for that. ie. class C {} class D { scope C c; } Sean
Jul 30 2007