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

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();
r.gc();

//One alternative to above code is until method from system class
System.gc();
// this does the same thing