Garbage Collection -Part 1-

Purpose

In order to think about the handling of instances that I was not aware of at the beginning stage, I will first suppress the basic specifications of Java such as garbage collection

What is garbage collection?

A function that manages the memory capacity allocated to the JVM when the program is executed and releases the memory area as appropriate to prevent memory leaks.

Why do I have to free up memory?

In programming, not limited to Java, as shown in Fig1, various variables and statements are stored in memory and processed by the CPU as appropriate. Of course, when it is stored, it consumes the memory capacity, so it is necessary to delete unnecessary variables and instructions so as not to exceed the capacity allocated to the program. ** **

→ If the capacity is exceeded ... (For Java) OutOfMemoryError appears and the program is forcibly terminated. 無題のプレゼンテーション.jpg

At this time, in C language etc., the programmer side had to explicitly release the memory area in the code, but in object-oriented languages such as Java and functional programming languages such as Haskell, it is shown in Fig.2. It has a function called garbage collection, which we will talk about this time, and it manages memory almost automatically. 無題のプレゼンテーション (1).jpg

Doesn't the programmer have to worry about anything if it does it automatically?

However, since the program is running even while the GC is running, it is a problem if unnecessary things are deleted properly. Therefore, a certain standard of "unnecessary" is set, and GC is operated according to it. In the case of Java, the judgment is made by the following factors.

** · Not referenced by any other element **

Therefore, memory leaks may occur depending on the design. In addition, the following problems may occur depending on the frequency of GC operation.

** ・ Processing is delayed due to frequent GC **

Since the application is temporarily stopped while the GC is running, problems occur if the GC occurs frequently. In addition, the GC stop time may simply become longer. Furthermore, although this will be described next time, there are different types of GC, and depending on the specifications, memory may not be released even if the GC operates, causing a memory leak.

Next time preview

What is GC in JavaVM?

Recommended Posts

Garbage Collection -Part 1-
G1 Garbage Collection in 3 Minutes
Java Performance Chapter 5 Garbage Collection Basics
☾ Java / Collection
Java9 collection
I tried to summarize about JVM / garbage collection