Q:

When to Garbage Collect

I have a piece of code that load a very big image in memory. So it seemed like a reasonable thing to call 
System.gc();

before loading the image. From what I can tell it works with no issues.
Yesterday i decided to use a pretty useful piece of software called FindBugs that scans your code and reports back issues that might cause bugs or generally not advised strategies. The problem is that this piece of code i mentioned gets reported. The description is this:

... forces garbage collection;
  extremely dubious except in
  benchmarking code

And it goes on to elaborate :

Code explicitly invokes garbage
  collection. Except for specific use in
  benchmarking, this is very dubious.
In the past, situations where people
  have explicitly invoked the garbage
  collector in routines such as close or
  finalize methods has led to huge
  performance black holes. Garbage
  collection can be expensive. Any
  situation that forces hundreds or
  thousands of garbage collections will
  bring the machine to a crawl.

So my question is : Is it NOT OK to programmatically call the garbage collector in such a case? My code only calls it once and the method that it is in gets used rarely. And if it is not OK to call it then what should you do in a case where you need as much memory as possible before doing a very memory intensive operation and you need to free as much memory as posible prior to it?

A:

Typically the GC is smarter than you, so it's better to let it run whenever the runtime decides. If the runtime needs memory, it'll run the GC itself

A:

Did you get any performance improvements with the System.gc()?
I don't think so, since you probably dont have a lot of objects that needs to be collected before you load the image.
Usually modern garbage collectors know best when to run, so you shouldnt force a collection, unless you have a really really good reason to. (for example a benchmarking application as suggested by that plugin)
btw: Calling System.gc() recommends the VM to perform a "full" or "large" collection, which means that all threads are stopped shortly. 
Otherwise it will probably only make "small" garbage collections, which don't stop all threads.
Run your program with -verbose:gc to see how many bytes are collected.
There is also lots of technical information on garbage collection here:
http://java.sun.com/developer/technicalArticles/Programming/GCPortal/

