What's the point of System.gc ()?

jvm has its own gc strategy, so what"s the point of using System.gc ()?

Jul.26,2021

actively prompt to reclaim memory

imagine such a scenario
you have 100m memory, and each operation requires 60MB. After each operation is completed, the system has 40m JVM that will not be actively reclaimed, but will OOM, if you apply for 60m for the second operation.

if you System.gc () once after the first operation is completed, the system can operate normally.

pseudo codes are as follows:

void bigmemop(){
    for(int i=0; i<100; iPP){
      use60M();
      System.gc();
    }
}

this happens when dealing with large Excel tables in a project. When memory is limited (when is it not limited?) Active control is still very important.

It obviously doesn't make sense for

PS:
code to rely on Gc, because gc may do nothing. If you don't know whether to use it or not, the answer is: don't use it!
but not all jvm are smart, and not all jvm default behaviors are the same. When there is no better way to deal with memory recycling, System.gc provides you with a choice and possibility.

many items are used:
such as , eclipse-jetty ,
jmbr3d/MeshBrowser.java

roughly boils down to the following scenarios:

  • long-running programs, such as servers, containers
  • Monitor memory usage in order to get more real data
  • allow users to intervene manually
  • create a large number of objects briefly, and then discard

UPDATE2:
I used gc in my project nearly 10 years ago, using JDK 1.5. The situation is also much more complicated than the above example, so as not to mislead everyone, mark it first.


so it is rarely used. I've never used it anyway.

-disagree with the downstairs answer.

in order to confirm the correctness of my judgment, I specifically checked some data, but in the data I looked up, I did not say that "calling System.gc () can prevent OOM."

the description of System.gc () on the JDK document is:

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

Note that you use "suggests" instead of "make".

there is also this question and answer on SO: https://stackoverflow.com/que.

Almost all of the answers in

mention that System.gc () only "suggests" garbage collection, not "one must" do garbage collection.

and the answer with the highest number of votes clearly stated one sentence:

If the JVM is about to throw an OutOfMemoryError, calling System.gc () won't stop it

so, @ Yujiaao, I expect your example to do this: plus System.gc () will not OOM, the effect of OOM without adding .

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1beaf60-31283.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1beaf60-31283.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?