...
The allocation of large objects is expensive, and in part because the cost to initialize their fields is proportional to their size. Additionally, frequent allocation of large objects of different sizes can cause fragmentation issues or noncompacting collect operations.
...
The garbage collector can be explicitly invoked by calling the System.gc()
method. Even though the documentation says that it "runs the garbage collector," there is no guarantee as to when or whether the garbage collector will actually run. In fact, the call only suggests that the garbage collector will should subsequently execute.
Irresponsible use of this feature can severely degrade system performance by triggering garbage collection at inopportune moments rather than waiting until ripe periods when it is safe to garbage-collect without significant interruption of the program's execution.
...
When an application goes through several phases, such as an initialization and a ready phase, it could require heap compaction between phases. Given an uneventful period, The System.gc()
method may be invoked in such cases, provided that there is a suitable uneventful period between phases.
...