[JVM] 14. Overview of Garbage Collection

Hello, everyone, I am a pig who is dominated by cabbage.

A person who loves to study, sleepless and forgets to eat, is obsessed with the girl's chic, calm and indifferent coding handsome boy.

If you like my text, please follow the public account "Let go of this cabbage and let me come".

14- Overview of Garbage Collection

1. What is rubbish

  • Garbage collection is not a companion product of the Java language. As early as 1960, the first Lisp language that began to use dynamic memory allocation and garbage collection technology was born.
  • There are three classic questions about garbage collection:
    • What memory needs to be reclaimed?
    • When will it be recycled?
    • How to recycle?
  • The garbage collection mechanism is Java's signature ability, which greatly improves development efficiency. Nowadays, garbage collection has almost become the standard configuration of modern languages. Even after such a long period of development, Java's garbage collection mechanism is still evolving. Devices of different sizes and application scenarios with different characteristics have put forward new ideas for garbage collection. Challenge, of course, is also a hot spot for interviews.

What is rubbish?

  • What is garbage (garbage)?
    • Garbage refers to an object that does not have any pointers in the running program. This object is garbage that needs to be recycled.
  • If the garbage in the memory is not cleaned up in time, the memory space occupied by these garbage objects will remain until the end of the application, and the reserved space cannot be used by other objects. It may even cause memory overflow.

2. Why do we need GC

  • For high-level languages, a basic cognition is that if garbage collection is not performed, memory will be consumed sooner or later, because memory space is constantly allocated without recycling, as if it is constantly producing household garbage and never cleaning.
  • In addition to freeing useless objects, garbage collection can also clean up record fragments in memory. Defragmentation moves the occupied heap memory to one end of the heap so that the JVM can allocate the defragmented memory to new objects.
  • As the business dealt with by the application is getting bigger and bigger. Complexity, more and more users, without GC, the normal progress of the reference program cannot be guaranteed. The GC of STW often fails to keep up with the actual demand, so it will continue to try to optimize the GC.

3. Early garbage collection

  • In the early C/C++ era, garbage collection was basically done manually. Developers can use the new keyword to apply for memory, and use the delete keyword to release memory.
  • This method can flexibly control the time of memory release, but it will give developers the management burden of frequent application and release of memory. If a memory area is forgotten to be recycled due to a programmer's coding problem, a memory leak will occur, and garbage objects can never be cleared. As the system running time continues to grow, the memory consumed by garbage objects may continue to rise until memory appears. Overflow and cause the application to crash.

Now, in addition to Java, C#, Python, Ruby and other languages ​​all use the idea of ​​automatic garbage collection, which is also a future development trend. It can be said that this automatic memory allocation and garbage collection method has become an indispensable standard for modern development languages.

Four, Java garbage collection mechanism

  • Automatic memory management, there is no need for developers to manually participate in the allocation and recovery of memory, which reduces the risk of memory leaks and memory overflows
    • Without a garbage collector, java will be the same as cpp, all kinds of dangling pointers, wild pointers, and leaking problems make you a headache.
  • The automatic memory management mechanism frees programmers from heavy memory management and can focus more on business development.

Worries:

  • For Java developers, automatic memory management is like a black box. If you rely too much on "automatic", then this will be a disaster, and the most serious one will weaken the problem of locating Java developers when the program has a memory overflow. And the ability to solve problems.
  • At this point, it is very important to understand the principles of JVM's automatic memory allocation and memory recycling. Only after we truly understand how the JVM manages memory, can we quickly locate and solve the problem based on the error exception log when we encounter OutOfMemoryError.
  • When it is necessary to troubleshoot various memory overflows and memory leaks, when garbage collection becomes a bottleneck for the system to reach higher concurrency, we must implement necessary monitoring and adjustments to these "automated" technologies.

Which areas should be concerned about recycling?

  • The garbage collector can collect the young generation, the old generation, and even the whole heap and method area.
    • Among them, the Java heap is the focus of the garbage collector.
  • In terms of frequency:
    • Frequently collect Young area
    • Less collection of Old area
    • Basically immobile Perm area

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/112716229