Does the GC prioritize same-class when looking for things to free?

cc cc at nevernet.com
Thu Sep 15 17:12:40 UTC 2022


Why is Foo never deallocated here? (`DMD32 D Compiler 
v2.099.0-dirty` win64)
```d
class Foo {
	string s;
	static size_t count, alloced, dealloced;
	this() {
		"+Foo".writeln;
		count++;
		alloced++;
	}
	~this() {
		"~Foo".writeln;
		count--;
		dealloced++;
	}
}
class Bar {
	string s;
	static size_t count, alloced, dealloced;
	this() { count++; alloced++; }
	~this() { count--; dealloced++; }
}
void main() {
	static assert(__traits(classInstanceSize, Foo) == 
__traits(classInstanceSize, Bar));

	GC.collect();
	writeln(GC.stats.usedSize, " START");
	{
		new Foo;
	}
	foreach (i; 0 .. 4_000_000) {
		new Bar;
		if (Foo.dealloced)
			break;
	}
	writeln(GC.stats.usedSize);

	writeln("Foo: ", Foo.count);
	writefln("Bar: %,d (%,d - %,d)", Bar.count, Bar.alloced, 
Bar.dealloced);

	GC.collect();
	GC.minimize();
	writeln(GC.stats.usedSize, " END");
```

```
0 START
+Foo
89632
Foo: 1
Bar: 2,800 (4,000,000 - 3,997,200)
64 END
~Foo
```


More information about the Digitalmars-d-learn mailing list