With regard to Java garbage collection, can garbage objects generated by a method not be collected after the method is executed?

the program first executes a start method to take up a lot of memory, and then executes endEmpty to call System.gc ()
to run, the task manager can see that the memory footprint of the program immediately reaches 4G, but the memory footprint of the start method does not decrease
after it is destacked. How can I free this memory?

and if you call System.gc () within the start method, but it works, when the method is useful and when it is invalid.

    static final long BEGIN_MILLIS = System.currentTimeMillis();

    public static void main(String[] args) {
        HelloWorld h = new HelloWorld();
        h.start();
        h.endEmpty();
    }

    public void start() {
        System.out.println("start");
        ArrayList<Object> list = new ArrayList<Object>();
        while (System.currentTimeMillis() - BEGIN_MILLIS < TimeUnit.SECONDS.toMillis(10)) {
            list.add(System.getProperties().clone());
//            System.gc();
        }
    }

    public void endEmpty() {
        System.out.println("end");
        while (true) {
            System.gc();
        }
    }

There should be two reasons for

:

  1. even if the memory of the java heap is reclaimed, it does not mean that java will immediately return the memory to the operating system.
  2. System.gc () does not necessarily trigger a gc operation.

in addition, monitoring java memory usage is best monitored using tools provided by java such as visualvm (jvisualvm) or jstat, rather than those provided by the operating system. Because the operating system only monitors how much memory is requested by the java program itself, but if java applies for so much memory, it will not necessarily use so much memory, nor will it necessarily switch to the operating system after it is used up.
you can learn more about the memory-related configuration.
Let me show you some pictures:

clipboard.png

clipboard.png

clipboard.png
clipboard.png


that's what I read in a book. I don't know if I can help you

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-1e4623d-6652f.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-1e4623d-6652f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?