Yes that defeats the purpose of using a garbage collector. What I am trying to ask is that is there a way to ask gcc to empty registers that contain variables that won't be used anymore in the program? By default, it just leaves those registers and those variables then only get garbage collected if something else gets written into those registers. So it is a highly variable behavior. I know that from an optimization point of view, gcc shouldn't care about these registers but it makes using a garbage collector very hard. Hamad ________________________________ From: Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx> Sent: Monday, May 13, 2019 1:16:38 PM To: Hamad Ahmed Cc: gcc-help@xxxxxxxxxxx Subject: Re: How to force gcc to blackhole registers so that things maybe garbage collected? On 2019-05-13 16:58 +0000, Hamad Ahmed wrote: > I am writing a C program and using the Boehm-GC for garbage collection. The GC > collects anything whose reference doesn't exist anymore. The problem is that > gcc will sometimes leave pointers to memory in the registers even though those > variables won't be used anymore in the program. This is a highly variable > behavior depending on register pressure and program size. But this messes with > the GC if a register holds a pointer to memory then that memory never gets > freed. We have tried putting x=NULL to force the register to be emptied but > gcc just ignores this because x is not used anymore in the program. Is there a > way that gcc blackholes registers if the variable they contain won't be used > in the program anymore? Without this functionality, using a garbage collector > is a nightmare. *(void* volatile*)&p = NULL; Though this looks stupid but it works: $ cat test.c #include <stdio.h> #include <stdlib.h> #include <gc.h> int main() { char *p = (char *)GC_malloc(256); scanf("%255s", p); printf("%s", p); p = NULL; *(void* volatile*)&p = NULL; return 0; } $ gcc test.c -S -O3 -fverbose-asm -o - | grep volatile # -fstrict-volatile-bitfields -fsync-libcalls -fthread-jumps # test.c:11: *(void* volatile*)&p = NULL; movq $0, 8(%rsp) #, MEM[(void * volatile *)&p] Oh, but if we know where we can put a "p = NULL" or the stupid thing I wrote, why not use a smart pointer based on refcount instead of GC? -- Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx> School of Aerospace Science and Technology, Xidian University