What is the garbage collector in Java?

前端 未结 16 1037
故里飘歌
故里飘歌 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 12:10

    The basic principles of garbage collection are to find data objects in a program that cannot be accessed in the future, and to reclaim the resources used by those objects. https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29

    Advantages

    1) Saves from bugs, which occur when a piece of memory is freed while there are still pointers to it, and one of those pointers is dereferenced. https://en.wikipedia.org/wiki/Dangling_pointer

    2) Double free bugs, which occur when the program tries to free a region of memory that has already been freed, and perhaps already been allocated again.

    3) Prevents from certain kinds of memory leaks, in which a program fails to free memory occupied by objects that have become unreachable, which can lead to memory exhaustion.

    Disadvantages

    1) Consuming additional resources, performance impacts, possible stalls in program execution, and incompatibility with manual resource management. Garbage collection consumes computing resources in deciding which memory to free, even though the programmer may have already known this information.

    2) The moment when the garbage is actually collected can be unpredictable, resulting in stalls (pauses to shift/free memory) scattered throughout a session. Unpredictable stalls can be unacceptable in real-time environments, in transaction processing, or in interactive programs.


    Oracle tutorial http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

    Garbage collection is the process identifying which objects are in use and which are not, and deleting the unused objects.

    In a programming languages like C, C++, allocating and freeing memory is a manual process.

    int * array = new int[size];
    processArray(array); //do some work.
    delete array; //Free memory
    

    The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.

    Step 2a. Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.

    To improve performance, we want to delete unreferenced objects and also compact the remaining referenced objects. We want to keep referenced objects together, so it will be faster to allocate new memory.

    As stated earlier, having to mark and compact all the objects in a JVM is inefficient. As more and more objects are allocated, the list of objects grows and grows leading to longer and longer garbage collection time.

    Continue reading this tutorial, and you will know how GC takes this challenge.

    In short, there are three regions of the heap, YoungGeneration for short life objects, OldGeneration for long period objects, and PermanentGeneration for objects that live during the application life, for example, classes, libraries.

提交回复
热议问题