Or does the compiler include some minimal garbage collector in the compiled program's code.
That’s an odd way of saying “the compiler links the program with a library that performs garbage collection”. But yes, that’s what’s happening.
This is nothing special: compilers usually link tons of libraries into the programs they compile; otherwise compiled programs couldn’t do very much without re-implementing many things from scratch: even writing text to the screen/a file/… requires a library. Or
But maybe GC is different from these other libraries, as mentioned in comments onwhich provide explicit APIs that the other answeruser calls?
No: in most languages, allocating dynamic storage for objects atthe runtime requireslibraries do a librarylot of behind-the-scenes work without public-facing API, beyond GC. Consider these three examples:
- Exception propagation and stack unwinding/destructor calling.
- Dynamic memory allocation (which is usually not just calling a function, as in C, even when there’s no garbage collection).
- Tracking of dynamic type information (for casts etc).
So a garbage collectingcollection library isn’t at all that special, and a priori has nothing to do with whether a program was compiled ahead of time.