. NET development process is not the program can not run, is the program's efficiency slow with the snail in the crawl; This situation is. NET's new hands are especially common; I don't know why, some introductions. NET development of the book cited in the example code, but also to ignore this problem, especially let me depressed is some of my favorite books also appear the same problem.
This article is not only for. NET Developer's Novice has the help, similarly to which has the experience, also brings some enlightenment and the reference.
What kind of problems will they encounter?
1. Database connection Timeout
2. Create objects to use, regardless of release
3. Debug (Debug) mode is compiled and used in the application environment.
4. Sharing the actual mode of operation
The problem above is like a cancer that accumulates to a certain extent and has far-reaching consequences.
1. Database Connection Timeout article
To know the database connection timeout problem, first look at the following code:
[SAMPLE-01]
The following are the referenced contents: Public Shared Function GETOEMPN (ByVal pspn As String, ByRef Oempn as String) as Bsresult Dim Clsresult as New bsresult Try Clsresult.resultid =-1 Dim Dtresult as New DataTable Dim Sql as String = String.Empty Dim clsoradb as New clsoracliendb Dim strconn as String = Configurationmanager.connectionstrings ("ConnectionString"). ConnectionString Clsoradb.open (strconn) ' Here Open, behind see Clsoradb.close SQL = "Select Satbmmbrnd. OEMPN Fruno from Satbmmbrnd WHERE satbmmbrnd. Matno =: Matno " Dim params () as Racleparameter = {New OracleParameter ("Matno", PSPN)} If clsoradb.filldatatable (SQL, dtresult, params) = False Then Return Clsresult End If If Dtresult is nothing Then Return Clsresult End If If dtResult.Rows.Count > 0 Then OEMPN = dtresult.rows (0) ("Fruno"). ToString () Else OEMPN = "" End If Clsresult.resultid = 1 Return Clsresult Catch ex as Exception Clsresult.resultid =-1 Return Clsresult End Try End Function |
A partial explanation of the above line of code:
Dim clsoradb as New clsoracliendb: A class referencing a database connection;
Clsoradb.open (strconn): Open the database connection;
Then, the entire function you can not find the action to close the database connection, is to wait for the operating system to release it? Some people say, it doesn't seem like a big deal, it's just a function; The database opens the connection and does not close does not affect the entire application; is that so?
Let's talk about the database connection, in the Oracle database, the general default number of database connections is up to 100 Torai, not more than 200, even if you change the number of connections, but anyway, its number of connections is limited, can not be unlimited for you to consume.
In the Web program, not only does it not automatically shut down the database connection, a function like this will reuse a database connection every time it is invoked; if there are many functions like this, you will be waiting for an error warning page to bounce out, such as database Connection Timeout ... and other information.
That's not much, and what's more, write the following code in a circular statement like this:
[SAMPLE-02]
The following are the referenced contents:
Foreach (DataRow row in Tabl.select ("", "ProductID") ............... Clsoradb.open (strconn) ............. Next Some people also like to play the following statement: [SAMPLE-03] Foreach (DataRow row in Tabl.select ("", "ProductID") Foreach (DataColumn col in Tbl.columns) ............... Clsoradb.open (strconn)
............. Next |
When it comes to this, someone asks, "I haven't tested a thing in the development environment." Yes, you are no problem; What I want to ask is, how many pieces of test data do you develop the environment?
Now, the question already knows where, how to solve?
For [sample-01] do the following, note the following code:
The following are the referenced contents: public Shared Function GETOEMPN (ByVal pspn As String, ByRef Oempn as String) as Bsresult Dim Clsresult as New BSR Esult Try Clsresult.resultid =-1 Dim dtresult As New DataTable Dim Sql as String = String.Empty Di M clsoradb as New clsoracliendb Dim strconn as String = Configurationmanager.connectionstrings ("ConnectionString"). ConnectionString Clsoradb.open (strconn) Note: After Open here, you can't see Clsoradb.close SQL =
"Select Satbmmbrnd. OEMPN Fruno from Satbmmbrnd WHERE satbmmbrnd. Matno =: Matno " Dim params () as Racleparameter = {New OracleParameter ("Matno", PSPN)} If clsoradb.filldatatable (SQL, dtresult, params) = False Then Return Clsresult End If If Dtresult is nothing Then Return Clsresult End If If dtResult.Rows.Count >
0 Then OEMPN = dtresult.rows (0) ("Fruno"). ToString () Else OEMPN = "
" End If Clsresult.resultid =
1 Clsoradb.close Note: see Clsoradb.close behind Return Clsresult Catch ex as Exception Clsoradb.close Note: Program exception also see Clsoradb.close Clsresult.resultid =
-1 Return Clsresult Throw EX End Try End Function |
Note the above two lines of code: Clsoradb.close and Clsoradb.close;
In exception handling, special reminders two points:
(1) Your database should be closed when the code line 0028 before, not after;
(2) Some people are not used to (or inadvertently) add 0088 lines of code;
for [Sample-02] and [sample-03], the Open database connection is written before all the loop statements, such as:
The following are the referenced contents:
Clsoradb.open (strconn) Foreach (DataRow row in Tabl.select ("", "ProductID") ...............
............. Next |
There is, of course, another way to commit with a using statement. NET application garbage collector collection automatically; There are a lot of related articles;
2. Object just create application, no matter release article
We continue with the [Sample-01] code:
Dim Dtresult as New DataTable
Who will find it released, you can't, I can't, never been released.
The code explanation for the "Dim Dtresult as New DataTable" line is to divide a space in memory to dtresult the defined object; How much space does the system divide? Yes, I haven't studied it. But there is a point, to partition a space in memory, is to occupy memory, then how much memory, not infinitely big bar; it is also limited; the end result of running the above code is that the system is performing more and more slowly; I have memory 1 to 2G, plus virtual memory is larger; I can only say that your suspicions are correct; but does your application use such a function? I don't think so. The application of hundreds function can be imagined; if it is a background autorun program, in time is a function, will let the system crash; This is just a simple example with more complexity; object applications like this: Datasets, Datatable , Datareader,dataadapter,datagrid. Wait
So how do you solve these problems?
(1) Define the object used before the try Catch statement; Such as:
The following are the referenced contents: Dim Dtresult as New DataTable Dim DR as New DataReader Dim DS as New Dataset Try .. Catch ex as Exception |
(2) The following statements are released:
The following are the referenced contents: Dim Dtresult as New DataTable Dim DR as New DataReader Dim DS as New Dataset Try .. ................. Catch ex as Exception --Releasing the Applied object Throw EX Finally --After use, release the applied object Dtresult.dispose--Clear from memory the |
Some people used to write the following:
The following are the referenced contents: Dim Dtresult as New DataTable Dim DR as New DataReader Dim DS as New Dataset Try .. ' After use, release the applied object Dtresult.dispose ' Clear the object from memory Dr.dispose ' Clear the object from memory Ds.dispose ' Clear the object from memory Catch ex as Exception |
This is not also released? What I want to ask is, if the program appears to be abnormal, will they be released? I must tell you that they must not be released. To ensure that the program works stably, I recommend that you use a try Catch statement.
(3) It is never recommended to write the following statement in a circular statement:
The following are the referenced contents: Foreach (DataRow row in Tabl.select ("", "ProductID") ............... Dim DS New Dataset Remember this is a big taboo to write code; Dim DT New databable .... ............. Next
There is also a way of writing Dim DS New Dataset Dim DT New databable ... Foreach (DataRow row in Tabl.select ("", "ProductID") |
The correct wording is:
The following are the referenced contents: Dim DS New Dataset Dim DT New databable ... Try Foreach (DataRow row in Tabl.select ("", "ProductID") Ds=nothing ' Every use, the memory space first released Dt=nothing ' Every use, the memory space first released Ds=getdatase Dt=getdatatable ......... . ............. Next Catch ex as Exception Throw EX Finally Ds.dispose Dt.dispose End Try |
Also, to remind you of this, remember to replace the for i=0 to Rowcount-1 with a for each statement;
3. Debug (Debug) mode is compiled for application environment medium
Let's look at the following picture:
Does anyone notice this interface? There is, but certainly not much.
The program is then developed (and also includes unit tests), which is then compiled and distributed directly to the application environment.
The whole process was over, and no one ever thought that there was a deep landmine here; according to Microsoft, this distributed program to the application environment, how much memory you have is not enough, so Microsoft advised us to do the following work:
"Please set debug and trace in Web.config as false." And all your programs, please ensure that compile is release Mode.
Application Set up for debugging
One reason for high memory so we are in Support a lot are when you have debugging, tracing, or both enabled for your Application.
While your are developing your application, this is a necessity. By default, if you create your application into Visual Studio. NET, you'll be the following attribute set in your WEB.C Onfig file:
and/or
Also, when you did a final build of your application, make sure so you to "release" mode, not "Debug" mode. ”
What would happen if we didn't? I share a colleague's feeling to everyone: "Already is very strong DB and AP server (all is Blade server), but the problem happened again and again, that feeling is really helpless very bleak ah ~"
Later found that the memory usage is very high, to a certain extent, the reaction slows down, this time as long as the restart of IIS can be a good time, and then analysis of IIS with the entity plus virtual memory more than 2G will explode;
That's why, do you want to meet? You might as well try.
4. The actual operation share article
The above three links, any one problem, will affect the efficiency of the system; I share some of the things that happen in the process of our actual operations and how to solve them.
(1) Memory use peak, resulting in the program can not continue to run;
One colleague shared their experiences as follows (share):
We have some programs that are run by the server and are more and more powerful. When you write a program, you may be less concerned about memory consumption.
The following example may give us a hint.
Here's the exact words:
Pls help check the "Run in Rack Job" program. It would no response after the running two or three days. The AP server Memory usage'll over 2.5G. After we close the "program, Memory'll decrease to 1.5."
The general meaning is: The server side (also called backstage) automatically run a program, run for two or three days, stopped running;
When checking the memory usage of the server, it was found to be over 2.5G; After shutting down the program, it dropped to 1.5G ... The following picture is for proof:
(2) Too many process requests, resulting in the CPU can not be processed in time, the program efficiency response is slow.
Here are the words of a colleague:
"After years of increasing production, new problems have arisen." From the server performance analysis, and the last memory is too high CPU usage. When the CPU is too high, the production line will respond to a large area of the response slow (this and connected to which AP has a relationship). Every time we're slow, we find the CPU-ap,recycle IIS application pool and it's OK. So I find Bon again to help analysis (conclusion: Microsoft closed the report 20090226v1-srt090119833891 Web service can ' t serve IISReset can fix.msg). Some suggestions are given when developing the program.
The conclusion is roughly that no process takes up a particularly high CPU, nor does the process take up too much CPU time. There are only too many processes for DB requests (the actual condition of the 3 factory is more than the attachment, the brush is fast), add up on the whole is too high. Many DLLs are also found to be built in debug mode, which takes up too much memory resources. Later, according to Bon's recommendations, we modified the IIS application pool settings as follows, to resolve too many requests can not be processed in time, resulting in high CPU problems. ”
Here are some questions and answers about the application connection pool (application pool) settings, to understand this setting has some help:
1. is one application pool ' s maximum memory usage 1.5G?
A&: Each application pool is a w3wp.exe. W3wp.exe is a process. Every process has 2 G User mode virtual address, so the maximum memory usage to application pool is 2G. However, you can ' t make sure so there is no memory fragment issue. Therefore, out of the memory always occur after 1.5 G according to our experience.
2. Are each application pool Independent on memory usage?
A&: Different application pools are different, so each w3wp.exe pool ' s application maximum memory is 2G.
3. Can Setup maximum CPU usage on each application pool?
A&: You can monitor it, but can ' t setup it.