What is the garbage collector in Java?

前端 未结 16 1038
故里飘歌
故里飘歌 2020-11-22 11:24

I am new to Java and confused about the garbage collector in Java. What does it actually do and when does it comes into action. Please describe some of the properties of the

相关标签:
16条回答
  • 2020-11-22 11:46

    Automatic garbage collection is a process where the JVM gets rid of or keeps certain data points in memory to ultimately free up space for the running program. Memory is first sent to heap memory, that is where the garbage collector (GC) does its work, then is decided to be terminated or kept. Java assumes that the programmer cannot always be trusted, so it terminates items it thinks it doesn't need.

    0 讨论(0)
  • 2020-11-22 11:51

    An object becomes eligible for Garbage collection or GC if it's not reachable from any live threads or by any static references.

    In other words, you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as the reference so if object A has a reference to object B and object B has a reference to Object A and they don't have any other live reference then both Objects A and B will be eligible for Garbage collection.


    Heap Generations for Garbage Collection -

    Java objects are created in Heap and Heap is divided into three parts or generations for the sake of garbage collection in Java, these are called as Young(New) generation, Tenured(Old) Generation and Perm Area of the heap.

    New Generation is further divided into three parts known as Eden space, Survivor 1 and Survivor 2 space. When an object first created in heap its gets created in new generation inside Eden space and after subsequent minor garbage collection if an object survives its gets moved to survivor 1 and then survivor 2 before major garbage collection moved that object to old or tenured generation.

    Perm space of Java Heap is where JVM stores Metadata about classes and methods, String pool and Class level details.

    Refer here for more : Garbage Collection


    You can't force JVM to run Garbage Collection though you can make a request using System.gc() or Runtime.gc() method.

    In java.lang.System

    public static void gc() {
        Runtime.getRuntime().gc();  
    }
    

    In java.lang.Runtime

    public native void gc();  // note native  method
    

    Mark and Sweep Algorithm -

    This is one of the most popular algorithms used by Garbage collection. Any garbage collection algorithm must perform 2 basic operations. One, it should be able to detect all the unreachable objects and secondly, it must reclaim the heap space used by the garbage objects and make the space available again to the program.

    The above operations are performed by Mark and Sweep Algorithm in two phases:

    1. Mark phase
    2. Sweep phase

    read here for more details - Mark and Sweep Algorithm

    0 讨论(0)
  • 2020-11-22 11:52

    Garbage Collector is part of JRE that makes sure that object that are not referenced will be freed from memory.
    It usually runs when you app runs out of memory. AFAIK it holds a graph that represents the links between the objects and isolated objects can be freed.
    To save performance the current objects grouped into generations, each time GC scans an object and finds that it is still referenced its generation count incremented by 1 (to some max maximum value, 3 or 4 i think) , and the new generation are scanned first (the shortest the object in memory the more probably it is no longer needed) so not all objects being scanned every time GC run.
    read this for more information.

    0 讨论(0)
  • 2020-11-22 11:56

    Many people think garbage collection collects and discards dead objects.
    In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage.

    When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system. To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm.

    Check this for more detail information: http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx

    0 讨论(0)
  • 2020-11-22 11:58

    To put it in the most simple terms that even a non-programmer can understand, when a program processes data it creates intermediate data and storage space (variables, arrays, certain object metadata etc.) for that data.

    When these objects are accessed across functions or over a certain size, they are allocated from a central heap. Then when they are no long needed, they need to be cleaned up.

    There are some very good articles online about how this works, so I'll just cover the very basic definition.

    The GC is basically the function that does this cleanup. To do this is clears table entries that aren't referenced by any active objects, effectively deleting the objects, than copies and compacts the memory. It's a little more complicated than this, but you get the idea.

    The big problem is some parts this process often requires the entire Java VM to require to be stopped temporarily to take place, as well as this entire process being very processor and memory bandwidth intensive. The various options as of GCs and tuning options for each one are designed to balance these various issues with the whole GC process.

    0 讨论(0)
  • 2020-11-22 11:58

    Garbage collector can be viewed as a reference count manager. if an object is created and its reference is stored in a variable, its reference count is increased by one. during the course of execution if that variable is assigned with NULL. reference count for that object is decremented. so the current reference count for the object is 0. Now when Garbage collector is executed, It checks for the objects with reference count 0. and frees the resources allocated to it.

    Garbage collector invocation is controlled by garbage collection policies.

    You can get some data here. http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

    0 讨论(0)
提交回复
热议问题