Build high performanceASP. NETSite 3 Details determine success or failure
It was once because of a small negligence that caused the server to crash. It was discovered that it was originally caused by a loop, I am deeply touched by the remark "Pay attention to details.
SeriesArticleLink:
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-monitor CLR Performance
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
The topics in this article are as follows:
Problem description
Importance of details
Problem description
First, describe the background of the story: (hope you can read the story patiently)
In the website, the page control in the webpage is displayed each time10Data entries. Each time you click the next page, the next page is retrieved again.10Data entries. As for how to do paging, there are many methods, and we believe that everyone knows this.
The process is as follows: when a user requests data (taking into account the user's operations and website traffic), I will retrieve the data for the first time.500And put the data in the cache.50The page data is stored in the cache, so that if the user requests the first page to49Page, the data is taken directly from the cache.
For example:
The first data block:
Saved in the form of a key-Value Pair: Dictionary
If the user requests49Page, the next data block (including501To1000Data), and now the memory has1000Data entries.
We will not talk about how long the cache will take, what data will expire, and what will happen after it expires. (The website runs well under this cache policy ).
CodeAs follows:
List < Product > Products = Getdatafromcacheordatabase (condition, pageindex, count ....);
The Code clearly indicates that data is obtained from the cache. If no corresponding data exists in the cache, the data is obtained from the database first.500Data entries are stored in the cache and finally returned.10Data entries.
Later, because of the needs of some features, you need to return to the front of the current page6Page data and post6Page data. For example, if the current page is12Page, then return12Before page6PageProduct(That is,6, 7, 8, 9, 10, 11Page data), and12PageProduct(13,14, 15,16, 17,18Page data ).
As follows:
Of course, if the current page is5Page5The data on the page is returned.5Page after6Page number.
This may involve cross-block data acquisition, for example:
If the current page is48When the page is displayed6There is no problem with the page data.6Page data is insufficient because49,40You can also obtain the data from the cached data blocks.51,52, 53,54Page data needs to be read from the database again, and then cached again (if not cached in advance ).
Finally, the data in the cache is as follows:
Then call the method: (pseudo code)
List < Product > Products = Getdatafromcacheordatabase (condition, 42 , 126 ....);
The above input is from42The data starting from the page, that is,48Before page6Page and back6Page data.
The internal implementation of this method is as follows:
1.First retrieve from the first data block42Page50Page data
Save the data inList <product> firstproductlist;
2.From the second data block51Page54Page (if the second data block does not exist in the cache, retrieve it from the database501-1000And then put it in the second cached data block ).
Save in the secondList <product> secondproductlist
3.ThenListMerge and return results. For example
Secondproductlist. foreach (u => firstproductlist. Add (u ));
The basic implementation is like this. It looks okay and reasonable, but this operation causes the server memory overflow.
Let's see why.
Importance of details
In fact, there are not many cached data, which is not enough to overflow the server memory, but the server still appearsOut of memory. It has been running well before, but the problem occurs only after the code is changed.
In fact, this is caused by a basic error: reference type.
The following is an analysis:
First, extract data from the first data block, and then use
List <product> firstproductlistReference the Retrieved Data
Then retrieve the data from the second data block and use
List <product> secondproductlistData Reference
For example
Use
Secondproductlist. foreach (u => firstproductlist. Add (u ));
SetSecondproductlistAdd dataFirstproductlistBecause it is a reference type, the actual operation result is: constantly changing the data in the first data block makes the data in the first data block more and more.
The current page is48Page, using the above operation, resulting in increased data in the first data block60Entries,
If you flip the page again49Page, then the data in the first data block increases60Entries
In the end, the server memory is insufficient, causing the server to crash. The original "hero"----The cache becomes the culprit.
To solve this problem, you only need to change the code a little bit:
List <product> firstproductlist;
List <product> secondproductlist;
Then
List <product> resultproductlist = new list <product>();Firstproductlist,SecondproductlistTraverse, addResultproductlistThat's all.
That's simple.
A small detail causes a big problem.
Do not ignore every detail. Do not perform operations such as loops without comparison. You must examine the code and refactor the code.
I wrote it here today, so I am so embarrassed that I hope to help you a little bit.
If there are any mistakes or you think they are inappropriate, I hope you can make an axe! And leave a high opinion! Thank you!
Copyright:XiaoyangAnd blog park all, you are welcome to reprint, reprint please indicate the source to the author.
Http://www.cnblogs.com/yanyangtian