It does, what it sounds exactly:
"Garbage collection"
Garbage collector has not a responsibility of memory allocation or ensuring there is enough memory to run program.
So take a look at wikipedia about garbage collector : https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
Also according to oracle :
What is Automatic Garbage Collection? Automatic garbage collection is
the process of looking at heap memory, identifying which objects are
in use and which are not, and deleting the unused objects. An in use
object, or a referenced object, means that some part of your program
still maintains a pointer to that object. An unused object, or
unreferenced object, is no longer referenced by any part of your
program. So the memory used by an unreferenced object can be
reclaimed.
https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
And check that stacoverflow questiont also : What's the difference between memory allocation and garbage collection, please?