Yes, the garbage collector is invoked automatically.
Actually, it doesn't quite wait for memory to run out. Instead, it tries to wait until the total ratio of "live" data to garbage is very small. This often happens when memory is almost full, but it also can happen early on in a program's execution, when very little memory is in use.
If you have an interest in this sort of thing, there are some upper-level CS courses that spend a bit of time on this, such as 15-312 and 15-411... |