www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How does the GC work?

reply Huang F Guan <gdxxhg+newsgroup google.com> writes:
// A simple program
int test()
{
	int *a = new int;
	*a = 1;
	return 0;
}

int main()
{
	test();
	return 0;
}

//======================
Hi, I wonder how the gc works in my D programs. This is a test program. In the
test function, I allocate a memory space for an integer pointer and don't
delete it by the end of the function. I think the garbage collector will help
me do this. But when I debugged my program, I couldn't find any deallocate
behaviors until the end of the test function. Finally I found that gc
deallocate it when the program was going to exit.

Doesn't gc deallocate the allocated momory at once when I no more use it?
Aug 23 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Huang F Guan wrote:
 // A simple program
 int test()
 {
 	int *a = new int;
 	*a = 1;
 	return 0;
 }
 
 int main()
 {
 	test();
 	return 0;
 }
 
 //======================
 Hi, I wonder how the gc works in my D programs. This is a test program. In the
test function, I allocate a memory space for an integer pointer and don't
delete it by the end of the function. I think the garbage collector will help
me do this. But when I debugged my program, I couldn't find any deallocate
behaviors until the end of the test function. Finally I found that gc
deallocate it when the program was going to exit.
 
 Doesn't gc deallocate the allocated momory at once when I no more use it?
 

It isn't a reference counting garbage collector. It's a mark-and-sweep type. That means that every so often it goes through the all the memory marks all the things that it finds references to (both on the GC heap and on the current stack), and deletes all the rest. The "every so often" part generally means during allocations. So the next time you use the GC to alloc something, it should delete the garbage. If you want the behavior of destruction as soon as it goes out of scope, then you can use a scope class. --bb
Aug 23 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Bill Baxter wrote:
 It isn't a reference counting garbage collector.  It's a mark-and-sweep
 type.  That means that every so often it goes through the all the memory
  marks all the things that it finds references to (both on the GC heap
 and on the current stack), and deletes all the rest.  The "every so
 often" part generally means during allocations.  So the next time you
 use the GC to alloc something, it should delete the garbage.

Actually, it should only do a mark & sweep when you ask it to allocate some memory, and it discovers that it's run out.
 If you want the behavior of destruction as soon as it goes out of scope,
 then you can use a scope class.
 
 --bb

Of course, this tragically doesn't work with pointers or arrays. The idiom I use is thus: { auto a = new int; scope(exit) delete a; // do stuff with a } This will cause a to be deleted at the end of the scope. -- Daniel
Aug 24 2007