You can garbage collect in Java simply by calling System.gc() but sometimes this \"stalls\" the application. Is it a bad idea to garbage collect like this and a
I totally agree that explicitely calling the garbage collector is not recommended in general. It can stall the application for several seconds and sometimes minutes. This is usually not an issue if you are dealing with a background service, but if it is exposed to users, they will have a bad experience.
However, there are extreme cases when you will want to do this: when memory is running really short and the application could crash. But a lot can be done is the application's design and implementation to avoid those situations.
One of the features of Java I love when it comes to recollecting memory and avoid memory leak are WeakReference objects. They are your friends.