Garbage Collector?

Mike Parker via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 27 18:28:01 PDT 2017


On Thursday, 27 April 2017 at 19:36:44 UTC, Ben wrote:
>
> Frankly seeing in this example that the GC was in theory able 
> to kick in 6 times in a simple 100 item loop, that is not 
> efficient. I if did my own memory management, the variable 
> cleanup will have been done in one go, right after the loop.

Please reread that bit.

"there were six total GC allocations in the loop. That means 
there were six opportunities for the GC to collect garbage."

Keyword: opportunities. There were six allocations, not six 
collections. The GC only "kicks in" when it needs to reclaim 
memory. In this case, the six allocations are made by the array 
management in DRuntime. It's the same as you allocating with 
`new` six times. In all of those cases, as long as the GC has 
room in its pool from which to allocate, there will be no 
collections.

Preallocate as much as possible and minimize the number of GC 
allocations (allocate on the stack where possible and through 
malloc where necessary) and the GC is not going to get in the way 
for most D programs you write.




More information about the Digitalmars-d mailing list