GC doesn't collect where expected

axricard axelrwiko at gmail.com
Mon Jun 19 18:01:16 UTC 2023


On Monday, 19 June 2023 at 16:43:30 UTC, Steven Schveighoffer 
wrote:
>
> In general, the language does not guarantee when the GC will 
> collect your item.
>
> In this specific case, most likely it's a stale register or 
> stack reference. One way I usually use to ensure such things is 
> to call a function that destroys the existing stack:
>
> ```d
> void clobber()
> {
>    int[2048] x;
> }
> ```
>
> Calling this function will clear out 2048x4 bytes of data to 0 
> on the stack.
>
> -Steve

Does it mean that if my function _func()_ is as following (say I 
don't use clobber), I could keep a lot of memory for a very long 
time (until the stack is fully erased by other function calls) ?


```
void func()
{
    Foo[2048] x;
    foreach(i; 0 .. 2048)
      x[i] = new Foo;
}
```



More information about the Digitalmars-d-learn mailing list