I’ll assume you are still curious :).
Yes and No.
Garbage Collection is necessary for a language like Java. With the way most programs are written, objects will be created and destroyed at runtime. Once an object no longer has any references, it is simply taking up space in RAM and ought to be removed. Failure to do so will result in “out of RAM” issues, even though your program is not actually using much RAM.
Garbage collection can take an indeterminate amount of time. Doing it randomly at runtime is generally bad. Robots depend on the fact that your periodic() functions get called at a regular 20ms rate. If you randomly have to wait 500ms for garbage collection to finish, you may notice your robot occasionally stutter or stop. This is a key historical argument against using Java, and is the reason why, professionally, I would never use a garbage collecting language for a professional, production real-time solution.
Messing with garbage collection can also produce weird side effects and is often not super effective. The smart folks who made the JVM did a good job at picking the “least evil” solutions for getting garbage collection working well - regardless of whether your application has to run real-time or not, no one likes code that randomly stutters.
The easiest answer within Java is to minimize the usage of the keyword
new. Do all your object creation in robotInit(), and don’t repeatedly
new objects at runtime, without good reason.
I have seen teams that attempt to use
System.gc(); to force a garbage collect operation while the robot is disabled, but I’m not clear on if this is actually effective, or a recommended practice.