www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - When does GC run?

reply John Burton <john.burton jbmail.com> writes:
Is there any documentation or information about the specifics of 
the garbage collector?

The information I have found indicates that it runs to free 
memory when the system runs out of memory to allocate. But will 
this try to use all the system memory or some other amount before 
trying to garbage collect? If I have a _lot_ of garbage, will it 
try to collect that before allocating any more system memory? Is 
this configurable in any way?

Any information or just pointer to information that I've somehow 
failed to find would be appreciated.
Oct 16 2018
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 16/10/2018 10:38 PM, John Burton wrote:
 Is there any documentation or information about the specifics of the 
 garbage collector?
 
 The information I have found indicates that it runs to free memory when 
 the system runs out of memory to allocate. But will this try to use all 
 the system memory or some other amount before trying to garbage collect? 
 If I have a _lot_ of garbage, will it try to collect that before 
 allocating any more system memory? Is this configurable in any way?
 
 Any information or just pointer to information that I've somehow failed 
 to find would be appreciated.
It is based upon how much is currently free and is not configurable. The GC of course doesn't just use all the system resources that would be stupid. It will collect long before that either when you tell it to (core.memory GC.collect) or when you do an allocation and it is enabled. GC series on the D blog: https://dlang.org/blog/the-gc-series/
Oct 16 2018
prev sibling next sibling parent Kagamin <spam here.lot> writes:
On Tuesday, 16 October 2018 at 09:38:44 UTC, John Burton wrote:
 Is there any documentation or information about the specifics 
 of the garbage collector?

 The information I have found indicates that it runs to free 
 memory when the system runs out of memory to allocate. But will 
 this try to use all the system memory or some other amount 
 before trying to garbage collect? If I have a _lot_ of garbage, 
 will it try to collect that before allocating any more system 
 memory? Is this configurable in any way?
Configuration options: https://dlang.org/spec/garbage.html#gc_config
Oct 16 2018
prev sibling parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Tuesday, 16 October 2018 at 09:38:44 UTC, John Burton wrote:

 The information I have found indicates that it runs to free 
 memory when the system runs out of memory to allocate.
No, that is incorrect. By default, here's what's happening: 1) At startup (or first 'new' or GC.malloc) the GC allocates a pool of memory. 2) Subsequent calls to 'new' or GC.malloc allocate from that pool. 3) If there isn't enough memory in the pool to allocate, it may either: a) Pause all your threads and perform a collection sweep, unless collection is disabled. If there's still not enough contiguous address space in the pool after that, it'll go to b) b) Ask the OS for more memory for the pool. Therefore, implicit automatic collection only occurs when you allocate, and only if needed. Explicit collection can be requested by calling GC.collect().
 But will this try to use all the system memory or some other 
 amount before trying to garbage collect? If I have a _lot_ of 
 garbage, will it try to collect that before allocating any more 
 system memory? Is this configurable in any way?
If you have a lot of garbage, you should probably allocate in advance, disable/enable collection around hot paths and explicitly ask for collection in between, or just not use the GC, i.e. allocate a block and do your business inside of it.
Oct 16 2018