In java garbage collection is internally implemented. For it, JVM(Java Virtual Machine) uses a Class

The JVM’s heap stores all objects created by an executing Java program. Objects are created by Java’s “new” operator, and memory for new objects is allocated on the heap at runtime. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.

You can also externally  set the max heap size for your JVM by the VM command
java -Xms .

If u   want to implement garbage collection in your program u can use the method of _ Runtime _ class there is a native method defined in java that is used to call a garbage collector.

Runtime r = Runtime.getRuntime();

//One alternative to above code is until method from system class
// this does the same thing