ABp16
ABp16
CC#
Created by ABp16 on 9/6/2024 in #help
Help me understand the GC in depth
I'm interested in the inner workings of the CLR GC but the source code looks very complicated to me and uses a lot of jargon I'm not familiar with. I've read the "surface level" design doc (https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/garbage-collection.md) but it doesn't contain the information I'm looking for. I'm particularly interested in the data structures it would use to keep track of objects during the compaction phase. Here are some questions I'm trying to find the answer to (along with answers that I tried to guess myself but could be completely wrong) : - how does it know the type of an object at a pointer ? is it present in some metadata header before each object in memory or does it know based on where it got the pointer from (aka, it knows the type of the object at the root so it knows the types of the fields, and then it would know the types of those fields, etc...) idk how that second approach would fit into the whole polymorphism thing though, so my guess is it's the first option. If it does use a header for each object, how big is it ? If I allocate a class containing one byte of data, is it going to add hundreds of bytes of metadata to it ? - how does it know how to iterate through all objects on the heap ? Again, one thing it could do would be to start at the roots and recursively explore the fields in those objects, but I have a feeling it's not the most efficient approach. My other idea would be that every object is prefixed with metadata (in some sort of header again) that would give its size so that it can start at the beginning of the heap and append - when compacting it needs to keep track of where each pointer will end up at after compaction, so that it can then correct those in every field of every object. Does it just use a hashmap from pointer to pointer for that ? What if I have a huge amount of very small objects on the heap ? Wouldn't that hashmap use more memory than my objects ?
10 replies
CC#
Created by ABp16 on 4/22/2024 in #help
Selecting an overloaded method based on the runtime type of an object by casting it to dynamic
Is this something I should do ? Is it a common pattern ? Are there any downsides / unexpected behavior I can encounter doing this ?
private ResultValue ProcessAstNode(AstNode.Expr.Block node)
{
...
}

private ResultValue ProcessAstNode(AstNode node)
{
// Select the right method depending on the runtime type of node
return ProcessAstNode((dynamic)node);
}
private ResultValue ProcessAstNode(AstNode.Expr.Block node)
{
...
}

private ResultValue ProcessAstNode(AstNode node)
{
// Select the right method depending on the runtime type of node
return ProcessAstNode((dynamic)node);
}
19 replies
CC#
Created by ABp16 on 4/20/2024 in #help
Explanation for array initialization peculiarities
I'd like to get an explanation for the following behavior :
Ty[] tys = {};
Console.WriteLine(tys.Length); // This is fine

// Of is a Ty[]
var tup = new Ty.Tuple { Of = {} };
Console.WriteLine(tup.Of.Length); // This causes a null pointer exception
Ty[] tys = {};
Console.WriteLine(tys.Length); // This is fine

// Of is a Ty[]
var tup = new Ty.Tuple { Of = {} };
Console.WriteLine(tup.Of.Length); // This causes a null pointer exception
Why does {} seem to do something different depending on where it is ?
85 replies