[Original] building a high-performance ASP. Net Site Chapter 7 how to solve memory problems (previous)-Managed Resource Optimization-in-depth analysis of the garbage collection mechanism

Source: Internet
Author: User

Build high performanceASP. NETSite Chapter 7 How to solve memory problems(Previous)-Managed Resource Optimization-Analysis of the garbage collection mechanism

This chapter describes in detail how to solve the performance problems caused by memory problems.. NetThe kernel is analyzed, and then a solution is provided.ArticleAnd strive to do the following.

 

This article paves the way for the future and is more brilliant. Only by truly understanding this article can we proceed smoothly.

 

The topics in this article are as follows:

Memory problems(Previous)

Managed Resource Optimization (previous)

Object lifecycle (previous)

Object"Generation "(previous)

Large Object heap(LOH)(Previous)

CLRCounter usage(Previous)

CLR profiler(Part I)

Different versions of the garbage collector (Part 1)

Object usage considerations (Part 1)

Common optimization measures (Part II)

Optimization of unmanaged Resources

SessionSession Optimization

 

Links to articles:

Build high performanceASP. NETSite Opening

Build high performanceASP. NETOne site Analyze the page processing process (front-end)

Build high-performance ASP. Net Site 2 optimize HTTP requests (front-end)

Building a high-performance ASP. Net site depends on the three details

Building a high-performance ASP. Net Site Chapter 5-Performance Tuning Overview (previous)

Design of large-scale high-performance ASP. NET System Architecture

Building a high-performance ASP. Net Site Chapter 5-Performance Tuning Overview (Part 1)

Building a high-performance ASP. Net Site Chapter 5-Performance Tuning Overview (Part II)

Building a high-performance ASP. Net Site Chapter 6-performance bottleneck diagnosis and preliminary optimization (Part 1)-identifying performance bottlenecks

Build a high-performance ASP. Net Site Chapter 6-performance bottleneck diagnosis and preliminary optimization (next article)-simple Optimization Measures

Building a high-performance ASP. Net Site Chapter 6-performance bottleneck diagnosis and preliminary optimization (next article)-reducing unnecessary requests

Building a high-performance ASP. Net Site Chapter 7 how to solve memory problems (previous article)-Managed Resource Optimization-in-depth analysis of the garbage collection mechanism

Building a high-performance ASP. Net Site Chapter 7 how to solve memory problems (previous article)-Managed Resource Optimization-monitor CLR Performance

 

 

Memory problems

AndCPUSimilarly, memory is an important hardware resource that directly affects server performance.

Generally, if the server memory is insufficient, the following two problems may occur:

1.As a result, the server writes some data originally to the memory to the hard disk. This not only increasesCPUAnd DiskI/OOperation, but also extended the time to read the data.

2.Some cache policies are blocked.

 

If the memory is insufficient, the fastest and most direct way is to buy a memory stick and add it to the server. However, there is a hidden problem: if the server is faced with insufficient memory after a new memory is added, it is impossible for us to add memory in an endless manner, therefore, we must solve this problem from the site itself, for example, from the server configuration, to the siteCodeAnalysis and Optimization.

 

Managed Resource Optimization

I believe you are no stranger to hosting resources. Simply putC #Managed resources created on the heap, or throughNewThe generated object.

Before giving an in-depth explanation, let's take a look"Object Lifecycle"

 

Object Lifecycle

When we useNewWhen the keyword creates an object, this object is assignedCRLOn the managed stack. The managed heap is in the memory. In addition, the object space allocation speed is very fast, because each time a certain amount of space is drawn at the end of the hosting heap to give this object, you do not need to find a suitable size on the heap.

If the managed heap allocates space for an object and finds that the space on the managed heap is too small to be allocated to the new objectCLRThen we started to run the garbage collection mechanism. We know that the garbage collection mechanism will clear all the objects without reference pointing to on the managed heap, and compress the existing objects on the managed heap.

However, it should be clear that some useless objects are cleared during garbage collection, but only when the next garbage collection is performed, only objects that were cleared in the previous garbage collection can be truly eliminated from the memory.(At this time, there are still some "Object recovery" topics that will not be described in detail).

 

The following describes some garbage collection topics.

 

Object"Generation"

in CLR During garbage collection, the garbage collector goes back to the managed heap to check whether the object can be recycled. This check process consumes a lot of resources. CLR use " generation " , for example, first generation and second generation. Then, each time you scan the managed heap conveniently, scan a certain " generation " .

In the managed heap, objects can be divided into three"Generation": 0Generation,1Generation,2Only these three generations. Every object is from0. Every time an object experiences garbage collection, and this object is still in use, the "Generation" of this object will increase.1Generation. For example0After a garbage collection, his generation is1Generation, if it is1The object to be replaced will eventually become2Generation. If the object itself is2No matter how many garbage collection times(If the object is always in use), So this object is still2Generation.

 

InCLRRemember the following words in garbage collection::"'Generation'The larger the number, the less likely it is to be recycled.". And some performance optimization is based on this.

 

Each timeCLRWhen garbage collection is performed, the first scan is performed first.0So some new temporary objects can be cleared immediately. In comparison, the garbage collector scans1The frequency of the object generation is no0Proxy, scan2The frequency of object generation is lower. Therefore, the longer the object remains, the more difficult it will be to be recycled and it will remain occupied.CLRMemory Resources.

 

Note that: IfCLRDecided to scan1And scan0And if,CLRScan2Object generation0Generation, number1All proxy objects will be scanned.

 

Therefore, we can conclude from this that we try to avoid turning the objects that need to be recycled immediately into objects that will survive for a long time. In general, if an object already exists0And then recycle it when used up. We don't want this object to survive1Generation, or even2Generation. In programming, this is basically the implementation idea: instantiate objects as late as possible, and release objects as early as possible.

 

Large Object heap(Large objecet heap)

we talked about " heap " except for the above general heap ( General New the heap where the object allocates space ) , CLR medium Another heap exists: it is used to place the heap of objects larger than 85 K , large object heap .

If New when an object is created, the object size exceeds 85 K , then CLR places this object in LOH above. If the LOH space is insufficient, CLR starts the Garbage Collector to scan LOH 2 On the heap and the general heap, as we have said before, if the 2 target object is scanned, scan the 1 generation, the 0 generation, it is equivalent to scanning the entire managed heap, and the performance impact can be imagined.

And you don't want the previous normal heap.LohAfter the above object is recycled by the garbage collector, the above large object will not be compressed, soLohThis heap may have some"Space fragments"When allocating new large objects, we need to find space and even sort out fragments. You can think of the disk fragmentation of our computer.

 

OKToday I will talk about it here. There are a lot of theories, but they all need to be clearly understood and mastered.

 

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.