0

My question is realy simple. I need to create 20.000 istances of a class, and I want to understand if it's best in terms of memory to put a method inside the instanciated class or if it's better to have only one method in the calling class, and then pass to it the single class properties I need to compute.

I mean, in terms of memory, it's best:

Scenario a)

public class b
{
    public string name;
    public string result;
}

public class a
{    
    public string myMethod(string name)
    {
    //do complex things... long method...
    }

    //create 20.000 istances of class b
    //and when I need it... I take one of them and do something like...
    myclassB.result=myMethod(myclassB.name)    
}

OR Scenario b)

public class b
{
    private string name;     
    private string result; 
    private string myMethod()
    {
        //do complex things... long method...
        result="something";
    }
}

public class a
{
    //create 20.000 istances of class b
    //and when I need it... I take one of them and do something like...
    myclassB.myMethod();
}

I mean, when I create 20.000 istances of the class with a method inside of it, will .net "duplicate" the method 20.000 times in memory ? Or it will only create one and then handle it by his own ?

BitQuestions
  • 651
  • 7
  • 14
  • 3
    No, the method itself is not part of the state of an object. Only the fields are relevant. – Jon Skeet Jul 07 '16 at 17:30
  • As Jon said - but another thing to consider is that if the method is long and complex, it probably should be implemented as several private methods, or possibly in several methods in a class of its own. – Matthew Watson Jul 07 '16 at 17:33
  • Thank you very much to all! You saved me hours of re-coding :-) – BitQuestions Jul 07 '16 at 17:36
  • In addition to the linked duplicate, see more generically: http://stackoverflow.com/questions/8204595/whats-the-method-representation-in-memory – Cody Gray - on strike Jul 07 '16 at 18:01

2 Answers2

3

In terms of memory usage, the two scenarios will work the same.

vidstige
  • 12,492
  • 9
  • 66
  • 110
  • This is correct, but I'd like it if you went into some detail as to why this is true. I do see Jon Skeet's left a comment so he may be preparing an answer also. – overslacked Jul 07 '16 at 17:33
0

You don't have to worry about this, even if it would matter, let's take it other way, a pointer to a function is worth around 2 bytes of memory, and let's assume only that your methods will need to allocate this memory for each of your objects. The application is taking now 20Kb of memory. And if this still loks like a lot to you, just consider that a simple hello world console application in C# takes up to 4Mb of memory when runed in release with debugger attached. this is 200 times more memory than in the case where all the methods will take the memory of a pointer to a function. This is only to give you an image about the hole thing behind the memory allocation you have to worry about or not.

The best thing you should do, especially because you are writing code in a managed environement, is to think about what your application needs to do, and about it's design.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76