How can you force garbage collection in java?

You cannot force Garbage Collection, but you can request for it by calling the method System.gc(). But it doesn’t mean that Garbage Collection will start immediately. The garbage collection is a low priority thread of JVM.

Garbage Collection is Automatic Memory Manager for the dotnet framework. It manages the memory allocated to the .NET framework. CLR takes cares about .NET framework. When a variable is defined, Its gets a space in the memory and when the program control comes out of that function the scope of variable gets ended, so the garbage collection acts on and memory will releases.