High-performance -GC2

With questions to think about! Hello everyone

The last time we talked about some of the basics of GC, thanks to big brother showed me a comment.

Configuration parameters

  Method on configuring the garbage collector is not a lot, it is recommended not to move freely, Configuring and tuning the garbage collector, to a large extent by the hardware configuration, behavior, available resources and program decisions. A handful of several parameters is also used to control the behavior of a very high level, depending on the type of program,

Workstation mode or server mode?

  Default garbage collection station mode employed. Workstation mode, all the GC run to trigger garbage collection thread priority (Priority praɪɔːrəti) are the same. For a single computer processor, a workstation model is the only option, configure other parameters are invalid

  In server mode, GC will create its own dedicated thread for each logical processor or processor core. It will remain pending before the threads of the highest priority, but the need for garbage collection. After completion of garbage collection, as these threads into hibernation again.

  In addition, CLR will create their own belly heap memory for each processor, each processor contains a small heap objects stack and an LOH. From the application point of view, there is only one logical memory heap. Your code is not clear which object belongs to a heap object reference would be a cross between all the heap.

  Multiple memory heap will bring a written benefits.

  1: garbage collection can be performed in parallel, each thread is responsible for recycling a garbage collection heap memory, which allows garbage collection rate significantly Kuaiyu workstation mode

  2: In some cases, memory allocation speed will be faster, particularly for LOH, as it will be allocated simultaneously in all parts of the heap.

Concrete placement

  

<gcServer enabled="true">

In the runtime node.

After all in the end with what is good? If the application is designed for running on a multiprocessor host you are ready, then select the server mode. So in most cases, can make garbage collection takes time to a minimum.

If you need to perform multiple managed share a host, garbage collection server mode creates multiple high priority thread. If multiple applications are set up so that the thread scheduler will adversely affect each other, if you are sure you want multiple applications using the same host server mode garbage collection, as well as a practice, it is to make the existence of a competitive relationship the applications are concentrated in a few designated processor running, so CLR will create its own memory heap for these processors.

Background garbage collection

First BackgroundGC only affect the behavior of 2nd generation garbage collection heap memory, generation 0 and generation 1 garbage collection garbage collection will still be using the front desk, which is a thread will block all applications. Background garbage collection is done by a dedicated second generation of garbage collection threads,

If it closes, but not recommended

<gcConcurrent enabled="false"/>

 

Low latency mode

If ensure high performance over a period of time, you can not perform GC notice significant overhead generation 2 garbage collection. Please GCSettings.LatencyModel according to other parameters to assign one of the following property values.

  LowLatency --- mode is only available with the workstation GC, prohibiting second-generation garbage collection.

  SustainedLowLatency-- apply GC workstation and server model, the second generation is completely prohibited garbage collection, but allows second-generation background garbage collection, you must turn on background garbage collection.

Because will not be defragmented, so these two parameters will significantly increase the size of the managed heap, if your process requires a lot of memory, you should avoid using this delay mode. Immediately before entering the low latency mode, it is best to enforce a full garbage collection, which can be completed by calling GC.Collect, after leaving the contemporary low-latency mode, immediately do a full garbage collection,

When the following conditions are met in order to open low-latency mode

  • Full garbage collection duration is too long, the normal operation of the program is absolutely unacceptable
  • Memory footprint of the application is much lower than the number of available memory.
  • Whether it is closed during the low latency mode, restart the program, or during a full garbage collection is performed manually, the application can remain a viable state.

How to reduce the amount of memory allocated

  To reduce the amount of allocated memory, a critical review did not object.

  • Really you need this object?
  • Are there any members of the object can be abandoned
  • Array can reduce the number?
  • Primitive types (Primitive) whether or not to reduce the volume (such as Int64 replaced Int32)?
  • Are some objects rarely used, making the assignment only when necessary?
  • Priority class can turn into a "structural" (Struct)? This will be stored on the stack, or to become a member of other objects
  • Allocate a lot of memory, if only a small part?
  • Whether to obtain the data in other ways?

The primary rule

  In fact, we already know, for the garbage collector, there is a basic high-performance encoding rules. In fact, the garbage collector is obviously designed according to this rule.

0 objects only memory heap garbage collection

  In short, the object lifetime as short as possible, so the garbage collector would not have to touch them, or can not fleeting, upgrade as soon as possible to let the object generation 2 heap and always stay where It will never be recovered. This means that should always be stored long-term survival of a heap object reference, usually it also means that objects should be reusable pooled (Pooling), especially LOH all objects.

Shorten the lifetime of the object

  • The smaller the scope object , the more no chance of being promoted to the next generation when garbage collection.
  • In general, the object should not be to allocate memory before use . Unless you create an object too much overhead. Need to create in advance that they will not affect the implementation of other operations,
  • Also in the use of objects, ensure that the object goes out of scope as soon as possible . For local variables, it may be after the last topical, even before the method can be ended.
  • If your code to perform multiple operations on an object, try to shorten the interval of the first and last used , so that the object can be recovered GC as soon as possible,
  • If an object is a reference to a long-surviving members of the object, sometimes you need to put this refers to a display set to null , this may slightly increase the complexity of the code a bit, because you need more than ready to check null value,

Reducing the depth of the object tree

  As we all know, GC will refer to the object along the traversal, the GC in server mode, there will be multiple threads simultaneously traversed once, you definitely want to take advantage of this concurrency as much as possible, but if there is a thread into a very long the chain of nested objects, the entire garbage collection process have to wait until the end of the thread after the completion of the entire work.

Decrement the reference between objects

If the object referenced a lot of other objects, the garbage collector when traversing it will take more time. If garbage collection temporary long time references, often means large, there is a reference to the relationship between complex object, the object is difficult to determine if all of the referenced relationship, there is a risk that is difficult to predict what the lifetime of objects, reducing the object reference complexity, not only beneficial to improve the code quality, but also allows code debugging and correcting performance problems easier, also need to pay attention to objects between different generations of memory heap references may lead to inefficient operation of the garbage collector, particularly new object is referenced from the old object, such as the second generation of the heap memory object contains a reference to an object of generation 0, so that each generation 0 garbage collection, there is always a part of the second generation of memory heap the objects have to be traversed. In order to confirm whether they still hold heap generation 0 object.

 

Guess you like

Origin www.cnblogs.com/ccaa/p/12571485.html
Recommended