Java garbage Collection Manual translation-What is garbage collection
At first glance, garbage collection should do its name-find and throw away the rubbish. In fact, however, it does the opposite, garbage collection records all the objects that are still in use, and then marks the other as garbage. With this in mind, we started digging into the details of how more Java virtual machines implement the automated memory recycling process known as garbage collection.
To avoid a plunge into detail, we start from scratch, explaining the general nature of garbage collection and the core concepts and methods.
Disclaimer: This manual focuses on the performance of Oracle hotspot and OPENJDK, and other runtimes and even other virtual machines, such as JRockit or IBM J9, may differ in some respects from the performance covered in this manual.
Manual memory Management
Before we start the modern garbage collection, let's take a quick look at the days when you need to manually display allocation and release data memory. Then if you forget to release the memory, you will not be able to reuse that piece of memory. This part of the memory is declared but not used, a scenario called a memory leak.
Here is a simple example written in C that uses manual memory management:
int send_request() { size_t n = read_size(); int *elements = malloc(n * sizeof(int)); if(read_elements(n, elements) < n) { // elements not freed! return -1; } // … free(elements) return 0;}
As we can see, it's very easy to forget to free up memory. Memory leaks have been a more common problem in the past than they are now. You can only really deal with them by fixing the code. Therefore, a better approach is to automate the work of reclaiming unused memory, eliminating the human error that may arise as a whole. This automated operation is called Garbage collection (GC).
Smart pointers
One way to automate operations is by using destructors. For example, we use vector vectors from C + + to do the same thing in the previous example, and it automatically calls its destructor when it leaves its scope:
int send_request() { size_t n = read_size(); vector<int> elements = vector<int>(n); if(read_elements(elements.size(), &elements[0]) < n) { return -1; } return 0;}
But in more complex cases, especially when sharing objects in multiple threads, using only destructors is not enough. This leads to the simplest form of garbage collection: reference counting. For each object, you only need to know how many times it is referenced, and when this count reaches zero, the object can be safely recycled. A well-known example is the shared pointer in C + + pointers:
int send_request() { size_t n = read_size(); auto elements = make_shared<vector<int>>(); // read elements store_in_cache(elements); // process elements further return 0;}
Now, to avoid reading this element elementsthe next time the function is called, we might want to cache them. In this case, it is no longer possible to destroy vector vectors when they are out of scope. Therefore, we use the shared pointer shared_ptr, which records the number of references to it when you use it to pass a value when the count is increased while the count decreases when it leaves the scope. Once the reference count drops to 0, the shared pointer shared_ptr automatically deletes the following vector.
Tbc...
Java garbage Collection Manual translation-What is garbage collection