Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it cost more memory to load classes with more methods?

Tags:

c#

Essentially, will more memory be used by instances of Foo when its value is acquired like this:

public class Foo
{

    internal double bar;

    double GetBar(){ return bar; }

}

or like this?

public class Foo
{

    internal double bar;

}  

public static class FooManager
{
    public static double GetBar(Foo foo) { return foo.bar; }
}

That is, is memory used per-method per-object or per-type?

like image 645
AlphaModder Avatar asked Mar 25 '15 04:03

AlphaModder


1 Answers

The instances themselves only have a single pointer to a table of methods that is loaded with the class, so there isn't extra per-instance overhead for having more methods, only fields.

The second example will use slightly more memory in total because you have definitions for two classes and so more per-class overhead (and as originally written, with FooManager as a non-static class, you need a FooManager instance), but the amount of memory used by each Foo instance is the same.

like image 129
Random832 Avatar answered Nov 01 '22 08:11

Random832