开发者

Object memory optimization question

Please pardon me for a n00bish question.

Please consider the following code:

public class SampleClass
{
    public string sampleString { get; set; }
    public int sampleInt { get; set; }
}


class Program
{
    SampleClass objSample;


    public void SampleMethod()
    {
        for (int i = 0; i < 10; i++)
        { objSample = new SampleClass();
            objSample.sampleInt = i;
            objSample.sampleString = "string" + i;

            ObjSampleHandler(objSample);
        }
    }

    private void ObjSampleHandler(SampleClass objSample)
    {
       //Some Code here
    }
}

In the given example code, each time the SampleMethod() is called, i开发者_开发技巧t would iterate for 10 times and allocate new memory space for the instance of SampleClass and would assign to objSample object.

I wonder,

  • If this is a bad approach as a lot of memory space is being wasted with it?

  • If that is the case, is there a

    better approach to reuse/optimize the allocated memory?

Or, Am I getting worried for no reason at all and getting into unneccesary micro optimisation mode ? :)

Edit: Also consider the situation when such a method is being used in a multi threaded enviornment. Would that change anything?


The technical term for what you are doing is premature optimization

You're definitely doing well to think about the performance implications of things. But in this case, the .NET Garbage Collector will handle the memory fine. And .NET is very good at creating objects fast.

As long as your class's constructor isn't doing a lot of complex, time-consuming things, this won't be a big problem.


Second option.

You shouldn't be concerned with this kind of optimization unless you're having a performance issue.

And even if you are, it would depend of what you do with the object after you create it, for example, if in ObjSampleHandler() you're storing the objects to use them later, you simply cannot avoid what you're doing.

Remember, "early optimization is the root of all evil" or so they say ;)


As you are creating a new object (objSample = new SampleClass();), you are not reusing that object. You are only reusing the reference to an instance of SampleClass.

But now you are forcing that reference to be a member-variable of your class Program, where it could have been a local variable of the method SampleMethod.


Assuming your code in ObjSampleHandler method doesnt create any non-local references to objSample, the object will become eligible for Garbage Collection once the method finishes, which will be quite memory efficient, and unlikely to be of concern. However, if you are having problems specifically with the managed heap because of this type of code then you could change your class to a struct, and it will be stored on the Stack rather than the Heap which is more efficient. Please remember though that structs are copied by value rather than reference, and you need to understand the consequences of this in the remainder of your code.

public struct SampleClass
{
    public string sampleString { get; set; }
    public int sampleInt { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜