Java should clean up memory by itself when it is needed, but if you’d like to give this a try, it should be as simple as running the code exactly as you suggested. Here is an example:
In the resources you link, Scott also pointed out the Run Garbage Collector node in the Vernalis community extension that basically does the same thing.
If you go with the aforementioned workflow, you can simply upload it to your Server and schedule it to run e.g. every hour.
Yeah, it should run safely at any time. It does consume some CPU time though, and in some cases may halt all executions while it is running. It shouldn’t run longer than around a second for each GB of memory cleaned up, though.