A detailed introduction to the garbage collection mechanism of Java and C #

Source: Internet
Author: User
Tags rounds
(a) The basic assumptions of the garbage collector

(1) objects that have recently been allocated memory space are most likely to need to be freed. Before a method is executed, it is often necessary to allocate memory space for the object to which the method is used, and searching for the most recently allocated collection of objects helps to free up the amount of free memory space that is likely to be spent.

(2) The longest-life object is least likely to be released. Objects that persist after several rounds of garbage collection are unlikely to be temporary objects that can be released in the next round of recycling, and the search for these blocks often takes a lot of work, but only a small portion of the memory space is released.

(3) objects that allocate memory at the same time are often used simultaneously. Connecting object storage locations where memory is allocated to each other can help improve cache performance.

(ii) several garbage collection mechanisms

(1) Mark-Clear collector

The collector first loops through the object graph and marks the reachable objects, then scans the stack for unmarked objects and frees up their memory. This collector typically uses a single thread to work and stops other operations.

(2) Tag-compression collector

Sometimes also called Mark-purge-compress collector, with the mark-purge collector has the same marking stage. In the second stage, the tag object is copied to the new domain of the stack to compress the stack. This collector also stops other operations.

(3) Copy Collector

This collector divides the stack into two domains, often referred to as half-space. Using only half of the space at a time, the new object generated by the JVM is placed in the other half of the space. When the GC runs, it compresses the stack by copying the reachable object to the other half of the space. This method is suitable for short-lived objects, and continuous replication of long-lived objects results in reduced efficiency.

(4) Incremental collector

The incremental collector divides the stack into multiple domains, collecting garbage from only one domain at a time. This can result in a smaller application outage.

(5) Generational collectors

This collector divides the stack into two or more domains for storing objects of different lifetimes. A new object generated by the JVM is typically placed in one of the domains. Over time, objects that continue to exist will receive a lifetime and go into a longer-lived domain. The generational collector uses different algorithms for different domains to optimize performance.

(6) Concurrent collector

The concurrent collector runs concurrently with the application. These collectors generally have to stop other operations to complete a particular task at some point (such as compression), but because other applications can perform other background operations, the actual time to interrupt other processing is greatly reduced.

(7) Parallel collector

The parallel collector uses some traditional algorithms and uses multithreading to perform their work in parallel. The use of multithreading on multi-CPU machines can significantly improve the scalability of Java applications.

(iii). NET Framework garbage collection mechanism

. The NET Framework consists of a managed heap, all of the. NET language is used when assigning reference type objects. Lightweight objects such as value types are always allocated in the stack, but all class instances and arrays are generated in a pool of memory that is the managed heap.

. The garbage collector in the NET Framework is known as the generational garbage collector (generational garbage Collector), which means that the allocated objects are divided into 3 categories, or "generations". The 0,1,2 are respectively. The initial size of the managed heap corresponding to 0, 1, and 2 is 256k,2m and 10M, respectively. The garbage collector changes the size of the managed heap when it finds that changing the size can improve performance. For example, when an application initializes many small objects and these objects are quickly recycled, the garbage collector will turn the No. 0 generation managed heap to 128K and increase the frequency of recycling. If the reverse is the case, the garbage collector will increase the size of the managed heap when it finds that it cannot reclaim much space in the No. 0 generation of managed heaps. All levels of the managed heap are empty until the application is initialized. When objects are initialized, they are placed in the No. 0 generation's managed heap in the order in which they are initialized.

Objects that were recently allocated memory space were placed in the No. 0 generation because the No. 0 generation was small enough to fit into the processor's level two (L2) cache, so the No. 0 generation was able to provide us with quick access to the objects. After a round of garbage collection, objects that remain in the No. 0 generation are moved into the 1th generation, and after a round of garbage collection, the objects that remain in the 1th generation are moved into the 2nd generation. The 2nd generation contains long-lived objects that have undergone at least two rounds of recycling.

When a C # program allocates memory for an object, the managed heap can almost immediately return the memory required for the new object, and the managed heap can have such an efficient memory allocation performance due to the simpler data structure of the managed heap. The managed heap is similar to a simple byte array and has a pointer to the first available memory space.

When a block is requested by an object, the pointer value is returned to the calling function, and the pointer is re-adjusted to point to the next available memory space. Allocating a block of managed memory is a little more complicated than incrementing the value of one pointer. This is also one of the performance optimizations that the managed heap optimizes. In an application that does not require too much garbage collection, the managed heap behaves better than the traditional heap.

Due to the existence of this linear memory allocation method, objects that are allocated concurrently in a C # application are usually assigned to each other adjacent to each other on the managed heap. The arrangement is completely different from the traditional heap memory allocation, which is based on the memory block size. For example, two concurrently allocated objects may be far apart on the heap, reducing the performance of the cache. So while memory allocations are fast, in some of the more important programs, the available memory in the No. 0 generation is likely to be completely consumed by the light. Remember, the No. 0 generation is small to fit into the L2 buffer, and memory that is not being used is not automatically freed. When there is no valid memory that can be allocated in generation No. 0, a garbage collection is triggered in the No. 0 generation, and all objects that are no longer referenced are deleted in this round of garbage collection, and the objects currently in use are moved to the 1th generation. Garbage collection for Generation No. 0 is the most common type of recycling and is fast. When the No. 0 generation of garbage memory is not effectively requested to sufficient memory, the generation of garbage memory recycling for the 1th generations is initiated. The 2nd generation of garbage memory recycling is to be used as a last resort, when and only if garbage memory recycling in the 1th and No. 0 generations cannot be provided with enough memory. If there is still no memory available after the generations have been garbage collected, a Outofmemeryexception exception is thrown.

(iv) Java garbage collection mechanism

How is memory placed when executing Java programs? Six places are mentioned in the book "Java Programming Ideas":

(1) Register (register)

(2) Stacks (stack)

(3) Heap: For placing all Java objects

(4) Static storage space (static storage): Used to hold data that persists during program execution. Decorated with STATCI.

(5) Constant storage space (Constant storage)

(6) Non-ram storage: I understand disk storage, which is a non-memory area.

Sun HotSpot 1.4.1 uses a generational collector, which divides the heap into three main domains: new, old, and permanent. All new objects generated by the JVM are placed in the new domain. Once an object undergoes a certain number of garbage collection loops, it gets the usage period and goes into the old domain. In a permanent domain, the JVM stores the class and method objects. In terms of configuration, a permanent domain is a separate domain and is not considered part of the heap. In this way, the JVM using the Hotspot engine technology should adopt a garbage collection mechanism similar to the. NET Framework-----The generational garbage collection method.

The following describes how to control the size of these fields. You can use-XMS and-xmx to control the entire heap's original size or maximum value.

The following command sets the initial size to 128M:

java–xms128m

–xmx256m to control the size of the new domain, you can use-xx:newratio to set the percentage of the new domain in the heap.

The following command sets the entire heap to 128m, the new domain ratio is set to 3, that is, the new domain is proportional to the old domain 1:3, and the new domain is 1/4 or 32M of the heap:

java–xms128m–xmx128m

–xx:newratio =3 can use-xx:newsize and-xx:maxnewsize to set the initial and maximum values for the new domain.

The following command sets the initial and maximum values of the new domain to 64m:

java–xms256m–xmx256m–xmn64m

The default size of the permanent domain is 4m. When you run the program, the JVM adjusts the size of the permanent domain to suit your needs. Each time the adjustment is made, the JVM performs a full garbage collection of the heap.

Use the-XX:MAXPERSIZE flag to increase the size of the permanent domain. When the WebLogic server application loads more classes, it is often necessary to increase the maximum value of the persistent domain. When the JVM loads a class, objects in the permanent domain increase sharply, allowing the JVM to constantly adjust the permanent domain size. To avoid adjustments, use the-XX:PERSIZE flag to set the initial value.

The following sets the permanent domain initial value to 32m and the maximum value to 64m.

java-xms512m-xmx512m-xmn128m-xx:permsize=32m-xx:maxpermsize=64m

By default, hotspot uses the replication collector in the new domain. This field is generally divided into three parts. The first part is Eden, which is used to generate new objects. The other two are called rescue spaces, and when Eden is full, the collector stops the application, copies all reachable objects to the current from rescue space, and once the current from rescue space is filled, the collector copies the reachable objects to the current to rescue space. From and to rescue space swap roles. The active objects will continue to replicate in the rescue space until they have been used and transferred to the old domain. Use-xx:survivorratio to control the size of new domain sub-spaces.

As with Newration, Survivorration prescribes the ratio of a rescue area to Eden space. For example, the following command sets the new domain to 64m,eden of 32m, each of which occupies 16m:

Java-xms256m-xmx256m-xmn64m-xx:survivorration =2

As mentioned earlier, the hotspot uses the replication collector for the new domain by default, and uses the tag-purge-compress collector for the old domain. Using the replication collector in a new domain has a lot of meaning, because most of the objects that the application generates are short-lived. Ideally, all transition objects will be collected when they are moved out of Eden Space. If this is the case, and the objects that move out of the Eden space are long-lived, you can theoretically move them into the old domain immediately, avoiding repeated duplication in the rescue space. However, applications do not fit into this ideal state because they have a small percentage of objects that are long-lived. It is best to keep these long-lived objects in the new domain, because copying a small portion of the object is always cheaper than compressing the old domain. To control the replication of objects in the new domain, you can use-xx:targetsurvivorratio to control the proportions of the rescue space (the value is the usage ratio for setting up the rescue space. If the salvage space is 1M, the value 50 indicates available 500K). The value is a percentage, and the default value is 50. When the larger stack uses a lower sruvivorratio, the value should be increased to 80 to 90 to make better use of the rescue space. The upper limit can be controlled with-xx:maxtenuring threshold.

The maxtenuring threshold can be set to 0 to place all replication occurrences and to extend the object from Eden to the old domain. After the setup is complete, the rescue space is not actually used, so the survivorratio should be set to the maximum to maximize Eden space, set as follows:

Java ...-xx:maxtenuringthreshold=0–xx:survivorratio=50000 ...

PostScript: As mentioned in "Java Chronicles Java Virtual Machine 10", "The last five years, is the (JVM) continue to optimize the five." There are several ways to continue the optimization, one is to study the new sampling algorithm. Because sampling is related to different optimization strategies, it can have a big impact on overall performance. The second is to study the method of depth optimization. Thirdly, the algorithm of garbage collection is researched. Garbage collection can lead to a brief pause in the program, which can lead to a negative user experience. Therefore, how to improve the efficiency of garbage collection, reduce latency, there are a variety of algorithms, such as progressive collection, train algorithm and so on. "Improving the speed and efficiency of language execution has always been the goal of design developers, so the algorithm of garbage collection will also evolve with the increase of the sun and Moon." I don't think any interviewer would dare to ask you about the garbage collection mechanism of C # or Java (at least I haven't met), and many of these issues can be written as lengthy agape once the discussion is deep. But the truth is a very beautiful thing, Confucius Deng Dongshan and small Lu, Deng Taishan and Small World.

The above is the Java and C # Garbage collection mechanism of the details of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.