For GC, when a programmer creates an object, GC starts to monitor the address, size, and usage of the object. Generally, GC records and manages all objects in heap by Directed Graphs. This method is used to determine which objects are "reachable" and which objects are "inaccessible ". When GC determines that some objects are "inaccessible", GC has the responsibility to recycle the memory space. Yes. Programmers can manually execute system. GC () to notify the GC to run, but the Java language specification does not guarantee that GC will be executed. Forcibly execute garbage collection: system. GC (). Runtime. getruntime (). GC ()
Static class: the static class belongs to the class rather than the object, which is equivalent to a global one and cannot be recycled.
Static variables are not recycled, but the objects referenced by static variables can be recycled.
GC only recycles the objects in heap, and the objects are the same. As long as there is no reference to it, it can be recycled (but not necessarily recycled ). object recycling has nothing to do with whether it is static!
For example, static vector pane = new vector (); pane = NULL; if there is no other reference, the object instance that the original pane points to will be recycled.
Java programmers no longer need to consider memory management when writing programs. Because of the garbage collection mechanism, objects in Java do not have the "Scope" concept, and only objects can be referenced with "Scope ". Garbage collection can effectively prevent memory leakage and effectively use available memory. The garbage collector is usually used as a separate low-level thread to clear and recycle objects that have been killed in the memory heap or are not used for a long time, programmers cannot call the Garbage Collector to recycle an object or all objects in real time.