. The whole performance of lifting system using single case mode in net

Source: Internet
Author: User

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.

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.