Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are pointers in D under the jurisdiction of the garbage collector?

I have a program which uses a lot of pointers for various things, and when I run it for long enough (meaning about 10 minutes), I start to consume an inordinate amount of RAM, which often causes it to stop running (the operating system kills it). This leads me to wonder whether pointers in D are under the jurisdiction of the garbage collector or not. Could someone please enlighten me?

like image 788
Koz Ross Avatar asked Dec 26 '22 05:12

Koz Ross


2 Answers

This leads me to wonder whether pointers in D are under the jurisdiction of the garbage collector or not.

Yes, they are. The garbage collector does not distinguish between pointers, dynamic arrays, references (ref arguments), or class references.

You may want to check for left-over pointers keeping unneeded data live, or false pointers (try targeting 64-bit if you're currently targeting 32-bit). If all else fails, you could switch to reference counting or manual memory management.

like image 45
Vladimir Panteleev Avatar answered Apr 28 '23 09:04

Vladimir Panteleev


Pointers may be scanned by the garbage collector, it depends on whether or not the garbage collector was instructed to do that. If you allocate memory with new or GC.malloc, then that memory can be collected by the garbage collector. If you allocate with regular C malloc, then that memory will not be managed by the garbage collector. GC has some other functions for controlling how memory will be managed if you require it, but it's typically not necessary.

I like to think of the garbage collector as being really dumb. It has some root pointers, it looks at the data referenced by those pointers to find other pointers to garbage collected data. If some data is orphaned, it can be freed. That's the essence of what it does.

like image 92
w0rp Avatar answered Apr 28 '23 09:04

w0rp