Part 1 of JVM_14_Garbage Collection Overview_Silicon Valley

1 What is garbage

image-20220625142718054

  • Garbage collection is not a byproduct of the Java language. As early as 1960, the first Lisp language that started to use memory dynamic allocation and garbage collection technology was born.
  • There are three classic problems with garbage collection:
    • What memory needs to be reclaimed?
    • When is it recycled?
    • How to recycle?
  • The garbage collection mechanism is a signature capability of Java, which greatly improves development efficiency . Nowadays, garbage collection is almost called 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 brought new challenges to garbage collection. Of course, this is also a hot spot for interviews .

Dachang interview questions

Ant Financial

Do you know which types of garbage collection periods, their respective advantages and disadvantages, focus on cms and g1

One side: What are the JVM GC algorithms, and what recycling algorithm does the current JDK version use?

One side: The G1 recycler talks about the recycling process

What is GC? Why is there a GC?

One side: Two judgment methods of GC? The characteristics of CMS collector and GC collector.

baidu

Let’s talk about the GC algorithm, let’s talk about generational recycling

Garbage Collection Strategies and Algorithms

Tmall

One side: jvm GC principle, how JVM reclaims memory

One side: CMS features, what are the garbage collection algorithms? What are the advantages and disadvantages of each, and what are their common disadvantages?

Didi

One side: What are the garbage collection periods of java? Let’s talk about the application scenarios of g1. How do you usually use the garbage collection period together?

Jingdong

Which kinds of garbage collectors do you know, their respective advantages and disadvantages, focus on cms and G1, including principles, processes, advantages and disadvantages. The implementation reason for the garbage collection algorithm.

Ali

Talk about garbage collection algorithm

Under what circumstances is garbage collection triggered?

How to choose an appropriate garbage collection algorithm?

What kinds of garbage collectors does the JVM have?

byte beating

What are the common garbage collector algorithms, and what are their advantages and disadvantages?

What will System.gc() and runtime.gc() do?

One side: Java GC mechanism? What are GC Roots?

Two sides: the recycling method and recycling algorithm of Java objects.

Do you understand CMS and G1? What problem does CMS solve, and talk about the recycling process.

CMS recycle paused a few times, why did it pause twice.

what is garbage

  • What is Garbage?
    • Garbage refers to an object that does not have any pointers in the running program , and this object is the garbage that needs to be recycled.
    • An Object is considered garbage when is can no longer be reached from any pointer in the running program.
  • If the garbage in the memory is not cleaned up in time, the memory space occupied by these garbage objects will be kept until the end of the application, and the reserved space cannot be used by other objects. It may even lead to memory overflow .

2 Why GC is needed

If you want to learn GC, you first need to understand why GC is needed?

  • For high-level languages, a basic understanding is that if garbage collection is not performed, the memory will be consumed sooner or later , because continuously allocating memory space without recycling is like constantly producing domestic garbage without cleaning it.
  • In addition to freeing unused objects, garbage collection can also clear record fragments in memory. Defragmentation moves the occupied heap memory to a section of the heap so that the JVM can allocate the sorted memory to new objects .
  • As the business dealt with by the application program becomes larger and more complex, and there are more and more users, the normal operation of the application program cannot be guaranteed without GC . And the GC that often causes STW can't keep up with the actual needs, so it will continue to try to optimize the GC.

3 Early Garbage Collection

  • In the early days of C/C++, garbage collection was basically done manually. Developers can use the new keyword for memory allocation and the delte keyword for memory release. For example the following code:

image-20220625150644224

  • This method can flexibly control the time of memory release, but it will bring the management burden of frequent application and release of memory to developers . If there is a memory area that is forgotten to be reclaimed due to programmer coding problems, then a memory leak will occur . Garbage objects can never be cleared until they pop up in memory and crash the application .

4 Java Garbage Collection Mechanism

  • Automatic memory management, no need for developers to manually parameterize and memory allocation and recovery, thus reducing the risk of memory leaks and memory overflow
    • Without a garbage collection period, java will be the same as cpp. Various dangling pointers, wild pointers, and leaks will give you a headache.
  • The automatic memory management mechanism frees programmers from heavy memory management, allowing them to concentrate more on business development
  • Introduction to Garbage Collection on Oracle's official website
    • https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/toc.html

to worry about

  • For Java developers, automatic memory management is like a black box. If you rely too much on "automatic", it will be a disaster, and the most serious will weaken the positioning of Java developers when the program has memory overflow. problems and problem-solving skills .
  • At this time, it is very important to understand the principles of JVM's automatic memory allocation and memory recovery. Only after we really understand how the JVM manages memory can we quickly locate the problem and solve the problem based on the error exception log when we encounter an OutOfMemroyError again.
  • When it is necessary to troubleshoot various memory overflows and memory leaks, when garbage collection is called the bottleneck of the system to achieve higher concurrency, we must implement necessary monitoring and adjustments for these "automated" technologies .

Should you be concerned about recycling in those areas?

image-20220625174414721

  • The garbage collector can recycle the young generation, the old generation, or even the full heap and method area.
    • Among them, the Java heap is the focus of the garbage collector.
  • In terms of times:
    • Collect Young Zones Frequently
    • Collect the Old area when less
    • Basically do not move the Perm area (or meta space)

Guess you like

Origin blog.csdn.net/weixin_43811294/article/details/125462311