Using closure causes GC allocation
Moritz Maxeiner via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 2 11:02:06 PDT 2017
On Saturday, 2 September 2017 at 17:43:08 UTC, Vino.B wrote:
> Hi All,
>
> Request your help on how to solve the issue in the below
> code as when i execute the program with -vgc it state as below:
>
> NewTD.d(21): vgc: using closure causes GC allocation
> NewTD.d(25): vgc: array literal may cause GC allocation
>
> void logClean (string[] Lglst, int LogAge) { //Line 21
> if (!Lglst[0].exists) { mkdir(Lglst[0]); }
> auto ct1 = Clock.currTime();
> auto st1 = ct1 + days(-LogAge);
> auto dFiles = dirEntries(Lglst[0], SpanMode.shallow).filter!(a
> => a.exists && a.isFile && a.timeCreated < st1).map!(a
> =>[a.name]).array; // Line 25
> dFiles.each!(f => f[0].remove);
> }
Line 25 happens because of `[a.name]`. You request a new array:
the memory for this has to be allocated (the reason why the
compiler says "may" is because sometimes, e.g. if the array
literal itself contains only literals, the allocations needn't
happen at runtime and no GC call is necessary). Since you don't
actually use the array, get rid of it:
---
void logClean (string[] Lglst, int LogAge) { //Line 21
if (!Lglst[0].exists) { mkdir(Lglst[0]); }
auto ct1 = Clock.currTime();
auto st1 = ct1 + days(-LogAge);
auto dFiles = dirEntries(Lglst[0], SpanMode.shallow).filter!(a
=> a.exists && a.isFile && a.timeCreated < st1).array; // Line 25
dFiles.each!(f => f.remove);
}
---
I cannot reproduce the line 21 report, though.
Since you use `timeCreated` I assume you're on Windows, but
what's your D compiler, which D frontend version are you using,
etc. (all the things needed to attempt to reproduce the error).
More information about the Digitalmars-d-learn
mailing list