1

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, it 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?

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Will the answer remain the same even for a multi-threaded approach ?
Absolutely. In fact, when doing multi-threading, if you can have a distinct set of data for each thread, that makes MT programming easier. But if you have, say, a collection of items to loop through and do some processing on each one - where no data is shared between the instances - multi-threading works well for that (and .NET has a new Parallel model for doing that sort of thing, too)
3

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 ;)

2 Comments

Well, lets say, the ObjSampleHandler is just processing the info in objSample and setting some value in a Collection object.. basically the objSample isnt required later.
Ok, but this is early optimization and you shouldn't be worrying about it. Besides, the way you're doing it now shows the intent in a clearer way (you're processing a different object each time) thus I think is better for readability having the object created with each iteration. Just my opinion though.
1

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.

2 Comments

Good point. Refactoring that field to a local variable makes the program easier to follow.
yeah, I know, I'm not reusing the same object.. but I wanted to know, if reusing it would improve the performance.. if yes, what should be the approach to reuse? Creating a method in 'SampleClass' to reset the values of its members, or as everyone else suggests, it would be an overkill.. Thanks.
0

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; }
}

1 Comment

Thanks Dean, but this is just a sample to illustrate the situation. In my actual implementation, I'll have to use Class only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.