Today, walking on the road suddenly reminded of a problem, before the profound attention.
Question: If an object is created frequently in the WebForm page cycle, and the request is destroyed after the end of the call, it will certainly have a certain performance impact on the system, and we all know that when we new an object each time, it allocates a space on the heap specified in memory, Then create this object and then destroy (Response.End () after execution) This object is sure to consume the server time and space (especially when the number of concurrent requests is very large, burst memory is not impossible), because each request will build the object. And then you have noticed that some classes may be like this, for example:
Public
class Peopleserver
{public
peopleserver () {} public
string tostringpeople (People p)
{ Return
string. Format ("Name:{0},age:{1};", P.name, P.age);
}
public class people
{public
string Name {get; set;}
public int Age {get; set;}
}
So when we need to print people each time, we estimate that we need
New Peopleserver ();
At this point, the server-side memory in the specified heap is allocated a piece of memory to store the newly created Peopleserver object. When the execution is finished destroying the object, think about it if our server can talk, then he's going to call you that. Every time you create a pair of the same, and each request needs to be created , you can not just create an object, we all use the same object when the operation, it is not a lot of things to save it?
Hey, you know what I mean? Next we design the code:
public class Peopleserver
{
private static peopleserver _people;
Public Peopleserver () {} public
string tostringpeople (People p)
{return
string. Format ("Name:{0},age:{1};", P.name, P.age);
}
The method must be static to facilitate other objects to invoke the public
static Peopleserver Getpeopleserver ()
{
if (_people = null)
{
_ People = new Peopleserver ();
}
Return _people
}
}
public class people
{public
string Name {get; set;}
public int Age {get; set;}
}
Is it a kind of epiphany to see getpeopleserver this method? So each time we go to format people this object is just a few lines of code:
People p = new people () {age =, Name = "Tongling"};
Peopleserver pserver = Peopleserver.getpeopleserver ();
Pserver.tostringpeople (P);
The extra overhead system for reducing the memory heap will certainly be faster, that's what we're going to say in a couple of days, like some configuration data, where the object information doesn't need to be changed after the first time, just the definition of some classes that need to get the object information. Knowing the principles will be able to improvise in future projects.