RingDotNetfor Ring

Memory and the garbage collector

.NET frees the memory of objects that nothing uses any more. This lesson looks at how that works — how much memory the program uses, when a collection happens, weak references for caches, and generations — and at what the bridge adds to the picture.

What you will learn#

The program#

samples/UsingDotNet/tutorials/ch06_memory.ring
# memory and the garbage collector - how much memory .NET uses, GC.Collect, WeakReference, generations and a big array
load "dotnet.ring"

using {
	"System"
	"System.IO"
	"System.Text"
}

func main

	oGC = new GC()

	# 1. .NET frees the memory of objects that nothing uses any more: the garbage collector (GC). The numbers depend on the computer and
	#    the moment, so the program prints only what is always true.
	nBefore = oGC.GetTotalMemory(True)
	? "1. the program uses memory: " + (nBefore > 0)
	? "   the number of generations: " + (oGC.MaxGeneration + 1) + "   (0 = new objects, 1 = in between, 2 = old objects)"

	# 2. A list with the capacity of 10 million bytes takes about 10 MB at once. GetTotalMemory(True) collects first and then answers.
	#    release() lets the bridge forget its handle, so nothing keeps the list alive.
	oBig = new List([ :typeArguments = ["System.Byte"], :capacity = 10000000 ])
	nWith = oGC.GetTotalMemory(True)
	? nl + "2. a list with room for 10 million bytes grew the memory by at least 9 MB: " + (nWith - nBefore > 9000000)
	oBig = NULL                     # nothing in Ring uses the list now: Ring frees it, and its handle is released with the next .NET calls
	callgc()                        # ... or at once: Ring collects its garbage (callgc) and the waiting handles are released (dnFlush)
	dnFlush()
	nAfter = oGC.GetTotalMemory(True)
	? "   after the list is dropped and a collection the memory is smaller again: " + (nAfter < nWith - 9000000)

	# 3. Collections are counted: CollectionCount(generation) says how many times the GC ran for that generation
	nCount = oGC.CollectionCount(0)
	oGC.Collect(NULL)
	? nl + "3. GC.Collect() ran a collection: " + (oGC.CollectionCount(0) > nCount)

	# 4. A WeakReference points to an object WITHOUT keeping it alive. It is the tool for a cache: keep it while somebody uses
	#    it, let it go when the memory is needed. IsAlive says whether the object is still there.
	oObject = new MemoryStream()
	oWeak = new WeakReference(oObject)
	? nl + "4. the weak reference is alive while we hold the object: " + oWeak.IsAlive
	oObject = NULL                  # the bridge keeps a handle to the object until Ring frees it: that is now
	callgc()
	dnFlush()
	oGC.Collect(NULL)
	oGC.WaitForPendingFinalizers()
	oGC.Collect(NULL)
	? "   after dropping it and a collection: alive = " + oWeak.IsAlive

	# 5. Which generation is an object in? A new object is in generation 0; after it survives a collection it moves up
	oNew = new StringBuilder("x")
	nGen0 = oGC.GetGeneration(oNew)
	oGC.Collect(NULL)
	nGen1 = oGC.GetGeneration(oNew)
	? nl + "5. generation of a new object: " + nGen0 + ", after one collection: " + nGen1 + " (it survived, so it moved up)"
Output
1. the program uses memory: 1
   the number of generations: 3   (0 = new objects, 1 = in between, 2 = old objects)

2. a list with room for 10 million bytes grew the memory by at least 9 MB: 1
   after the list is dropped and a collection the memory is smaller again: 1

3. GC.Collect() ran a collection: 1

4. the weak reference is alive while we hold the object: 1
   after dropping it and a collection: alive = 0

5. generation of a new object: 0, after one collection: 1 (it survived, so it moved up)
Note

This program uses a generic function (dnNewGeneric) where a class cannot be used. The lesson The generic functions explains them.

How it works#

The garbage collector#

In .NET you never free an object by hand. The GC finds the objects that no variable can reach any more and frees them. You do not choose the moment: it runs when memory gets short, or when the runtime decides. GC.Collect() forces a collection, and GC.GetTotalMemory(True) collects first and then returns the number of bytes in use. Both are useful for learning and testing; in a real program calling Collect is almost always wrong, because the GC knows better than you when to run (and a forced collection can slow the program).

The numbers (bytes in use, how many collections) differ on every computer and every run, so the program prints only statements that are always true, such as "it grew by at least 9 MB".

What the bridge adds#

When a .NET method returns an object to Ring, the bridge keeps a handle to it in a table, because Ring only holds a number. A reference in a table keeps the object alive, so the GC could not free it. That is why every Ring object that owns a handle also owns a small guard: when Ring frees the object (the last variable that holds it is changed or goes away), the guard puts the handle on a list, and the list is sent to .NET in one request. After that the GC is free to take the memory. You do not have to do anything: a loop that makes 100 000 objects and drops each of them stays small.

Two things remain your job:

  1. Dispose() (the tutorial Releasing resources) frees resources such as files and connections at once. Dropping the object frees its memory, but only some time later.
  2. Timing. Two things delay the release a little: Ring frees an object some moments after the last variable that held it is gone, and the handles are released in groups (the next .NET call does it when 32 are waiting). So the memory of one big object is not free at the moment you drop it. When it must go now, or before you look at the memory (GC.GetTotalMemory), write callgc() (Ring collects its garbage; it works for the function that calls it) and then dnFlush() (the waiting handles are released). release() is the other way: it releases one object immediately, and after it the object cannot be used any more.

Step 2 of the program shows the effect: a list with room for 10 million bytes makes the memory grow by 10 MB, and after the variable is set to NULL, callgc(), dnFlush() and a collection the memory is smaller again.

Weak references#

A normal variable keeps its object alive. A WeakReference points to an object without keeping it alive: IsAlive says whether the object is still there, and when the GC frees it, the answer becomes false. This is the tool for caches: keep the result of a slow calculation as long as memory allows, and calculate it again when it is gone. In step 4 the object is alive while Ring holds it; after the variable is set to NULL, callgc(), dnFlush() and a collection it is gone. (Collecting twice, with WaitForPendingFinalizers in between, makes the result sure in a small test; a real cache must always handle both answers.)

Generations#

The GC sorts objects by age, because most objects die young. Generation 0 holds new objects and is collected often and fast. An object that survives a collection moves to generation 1, and later to generation 2, which is collected rarely. GC.GetGeneration(object) tells the generation: a new object is in 0 and after one collection it is in 1, as step 5 shows. Big objects (more than about 85 000 bytes) go to a separate heap and are in generation 2 at once.

Note

GC.GetTotalMemory counts the memory of .NET objects only. The memory of the Ring program itself (its lists and texts) is separate and is not in the number.

What you learned#

Exercises#

  1. Make 100 000 StringBuilder objects with a capacity of 4000 in a loop and look at GC.GetTotalMemory after the loop, with and without callgc() and dnFlush().
  2. Build a small cache of results with WeakReference objects in a Ring list.
  3. Find out how many collections of generation 0, 1 and 2 happened in your program (CollectionCount).
  4. Read about GCSettings.IsServerGC and GCSettings.LatencyMode and print them.

Next#

The Windows Registry

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.