What is the Garbage Collection in Java?
- 12 extremely useful tricks for JavaScript programmers
- Arrays and objects in JavaScript are like stories and newspapers!
- Instructions on how to run / open Java files (.jar)
In garbage , garbage means that objects are no longer referenced and the garbage collection is used to perform automatic memory recovery. In other words, it's a way to destroy unused objects.
What do the Garbage Collection in Java have? Join TipsMake.com to learn about Garbage Collection concept, how it works and why it is important in this article!
1. Garbage Collection concept
Garbage Collection in Java is defined as a process of automatically executing memory management tasks.
Java code is translated into bytecode and then run on Java virtual machine or abbreviated as JVM. During the process of running the program, objects are created in the heap memory, part of the memory for the program. After all, there will be some objects that the program doesn't need to use. These objects will be scanned and deleted by garbage collector to recover memory capacity.
This free space will be allocated to new objects. With programming languages such as C, the release of memory is done manually (by initializing commands, freeing memory). With Java, memory release is done automatically.
See also: Write and run Java code on the computer for the first time
2. How does Garbage collection in Java work?
Garbage Collection in Java is a completely automated process. Programmers do not need to call memory clearing commands like in C / C ++ programming language. The processing system part of garbage collection is in the JVM. Each JVM has a way to install different garbage cleaners, which are consistent with those of the JVM. Among the JVM representatives, Oracle HotSpot will be taken for example by us because it is most commonly used and the HotSpot garbage cleaner has more powerful features than other brothers.
HotSpot has many garbage cleaners, but in general, these garbage cleaners all operate according to the general model. First, identify and mark unreferenced objects ready for garbage collection. Second, delete the marked objects. Optionally, the memory can be "compressed" again after the garbage collector deletes the objects, which means that the active objects will be located in adjacent cells at the beginning of the heap. . This "compression" process makes it easier to allocate memory for new objects after the memory block is allocated to existing objects.
All HotSpot garbage collectors follow a common tactic that divides the object by "active age". The reason behind the division of garbage collection by age is because most objects are short-lived and will be ready to collect immediately after creation.
According to the chart above, we see the age of the objects are divided into 3 age groups: Young generation - Young generation, Old generation - Old generation and Permanent generation - Immortal generation.
- Young generation - Young generation : This group is divided into 2 subgroups of Eden (Beginning) and Survivor (Survive). In the Survivor group are divided into two smaller groups, S0 and S1. Initially, the newly created objects will be in the Eden group. After an operation cycle of garbage collector, the "survivor" object will be transferred to the Survivor group. The event objects in the Young Generation group that are recovered by Garbage collector are considered Minor events. After the "multiple" scan cycle where the object is still in use, they will be transferred to Old generation memory.
- Old generation - The old generation : This group contains moving objects from Young generation (of course with a long enough running time, each garbage collector will define how much is considered "long"). The event that objects in the Old generation group are recovered by garbage collector is considered a Major event.
- Permanent generation - Immortal generation : This group includes metadata (eg classes, methods, .). Therefore, when you need to "remove" unnecessary classes and methods, garbage collector will search in this group.
In a garbage collection event, unused items in all generations are garbage collected. HotSpot contains 4 different Garbage collector sets:
- Serial : The garbage collection event is processed sequentially on one thread. Memory consolidation (compaction) is executed after each garbage collection event. Serial is the simplest garbage collector, designed for single thread environments. When Serial works, the application must stop. Therefore, it is not suitable for server environment.
- Parallel : Parallel is the default garbage collector of Java. The event minor will be processed on multiple threads. Major events and processes that stack memory for Old generation groups are processed on one thread. Besides the Parallel set, there's another set called Parallel Old that handles Major events and the process of cornering (for Old generation groups) on multiple threads. However, when operating Parallel, it will stop the thread from running the program.
- Concurrent Mark Sweep (CMS) : CMS is a combination and improvement of Parallel and Parallel Old. He dealt with minor events on multiple threads (similar to Parallel) and handled major event on multiple threads (similar to the Parallel Old). In addition, the CMS runs parallel to the application and ensures that the garbage removal process does not affect the application execution process. CMS does not carry memory. CMS consumes more CPU resources but does not affect application execution (also known as Stop The World - STW). For servers or applications that are at a disadvantage when having to use STW, using CMS is a suitable choice.
- Garbage First (G1) : This is the latest garbage collector, launched with Java 7 to replace CMS in managing heap areas> 4GB. The G1 uses multiple background threads to scan through the heap (divided into regions of 1 to 32MB). G1 will clean up in memory areas with the most "garbage". The G1 has the ability to recover and stack memory, and CMS can only stack memory in STW state. Furthermore, G1 has the ability to identify duplicate characters in the heap (referenced by different objects) and edit references to avoid redundant copies of strings, saving free space for heap memory.
See also: 5 interesting features about Java 9 you may not know yet
3. Benefits of garbage collection in Java
C / C ++ programming must have had trouble coping with Memory Leaks. However, with Java in particular and Garbage Collector-owning languages, this extreme has been completely terminated.
While programmers are still debating the ability of Garbage Collector to work effectively, the conservative side insists that manual memory management will bring better control and performance, Garbage Collector is becoming the "standard" feature of many new programming languages.
For Java, when the garbage collector operation is at risk of affecting application performance, programmers can customize garbage collector in different ways to achieve the desired performance.
See also: 9 things Java programmers should know in 2018 if they want a successful career
4. Work with the garbage collector so that it is effective?
With simple applications, programmers don't need to pay much attention to garbage collector. However, if they want to improve their own ability, understanding the mechanism of operation and customizing the garbage collector is one of the skills to be learned.
Besides garbage collector's mechanism, programmers need to keep in mind one thing, which is that the garbage collection in Java is unspecified and cannot predict when garbage collector will run. Even if you explicitly call it with System.gc () or Runtime.gc (), you still can't be sure if the garbage collector is running.
For custom garbage collector, the best approach is to adjust the JVM settings flags (which are the parameters corresponding to each garbage collector listed above). The flag can adjust the garbage collector (eg Serial, G1, etc.), specify the size when allocating and the maximum size of the heap device that the program uses, or adjust the size of each group. "year old". For example, the garbage collector Parallel is effective but will cause a "stop the world" problem, making it more suitable for auxiliary processing, where it is allowed to pause for garbage collection.
On the other hand, the garbage collector CMS is designed to minimize downtime, making it ideal for GUI applications where the response is critical. Additional adjustments can be made by changing the heap size or parts of it and measuring the efficiency of garbage collection using a tool like jstat.
Refer to some more articles:
- Top 5 popular CSS Framework that you should keep in mind
- Instructions on how to compile and execute Java using Command Prompt
- Statistics of programming languages that are "hated" most
Having fun!
You should read it
- What is Java? Why choose Java?
- Which career Java programming options are waiting for you?
- Basic Java syntax
- Test on JAVA test P5
- eQuiz - Multiple choice quiz about JAVA
- JAVA test on P6
- Basic Java exercises, with sample decoding
- JAVA test on P4
- JAVA test on P3
- Chinese scientists revealed plans for the Laser Station to clean up the garbage
- How to Set Up a Java Programming Environment
- In the end, big universities realized that Java was a lousy language if used for introductory programming
Maybe you are interested
Garbage falls from space and hits civilian homes in the US
Microsoft is developing a genuine 'garbage removal' tool for Windows
Top 8 best garbage cleaning apps on Android
The surprise: The applications that clean up the garbage in your phone are garbage apps, this is evidence
Tool to eliminate garbage web from search sources
6 steps to 'hide' garbage on Windows?