Methods within many instantiated classes?

J

Joe

I have a fundamental question about classes (and structs for that matter).
Suppose I have a class with a bunch of members and methods. And this class
will be instantiated many many times at once in memory. Now, performance
wise and memory consumption wise, which method would be best to design the
class? My underlying concern is that method #1 will consume more memory
since each class instantiated will be larger than method #2. Is this a
false assumption?

//Method 1: (keep methods inside class)
public class MyRectangle
{
double Width;
double Height;
public MyRectangle(double w, double h)
{
Width = w;
Height = h;
}

public double CalculateArea()
{
return (Width * Height);
}
}


//Method 2: (keep methods in another class)
public class MyRectangle
{
double Width;
double Height;
public MyRectangle(double w, double h)
{
Width = w;
Height = h;
}
}

Public class MyRectangleCalculations
{
public double CalculateArea(MyRectangle rect)
{
return (rect.Width * rect.Height);
}
}
 
A

Andreas Håkansson

Joe,

The extra memory bagage for placing the methods in the same class is so
small that you would have to create MANY objects for you to have to be
anywhere close to concerned. You what you would win in memory you
would probably lose in performance when you pass classes and return
values back and forth.

HTH,

//Andreas
 
G

Guest

This isn't a direct answer to your question, but method #2 goes against the concept of object-oriented design. I think using this approach would lead to applications that are difficult to understand and maintain

method #1 is far superior in terms of overall design, in my opinion.
 
P

Phil S

Hey;

Methods do not use any extra memory per instantiation. #1 is the more
object-oriented approach, but that's just stylistic.

If you like terminology, #2 is called a "utility class". It's great for
people who miss ANSI C. :)

Usually the methods in #2 would be marked "static", which means that you
don't actually have to instantiate a MyRectangleCalculations object to use
the method.
 
C

C# Learner

Joe said:
I have a fundamental question about classes (and structs for that matter).
Suppose I have a class with a bunch of members and methods. And this class
will be instantiated many many times at once in memory. Now, performance
wise and memory consumption wise, which method would be best to design the
class? My underlying concern is that method #1 will consume more memory
since each class instantiated will be larger than method #2. Is this a
false assumption?

Yes, in theory. Class method bodies are not contained in instances of
classes.

Maybe someone else can provide a full explanation of the implementation
of methods in .NET, but my understanding of this in general goes
something like this:

<code>

class Class
{
int i;
bool b;

public void Method()
{
}
}

</code>

The above would compile down to something along the lines of:

<code>

class Class
{
int i;
bool b;
}

public void Class_Method(Class this)

</code>

Consider the following code.

<code>

static void Main()
{
Class c = new Class();
c.Method();
}

</code>

What really happens "behind the scenes" will be something along the
lines of:

<code>

static void Main()
{
Class c = new Class();
Class_Method(c);
}

</code>

<snip>
 
A

Andreas Håkansson

Charlie,

I think method two would be suiteable if you had a polymorphic
situation,
where you would write a support class to manipulate the classes, perhas with
static methods. Other than that I would not recommend it either.

//Andreas

Charlie said:
This isn't a direct answer to your question, but method #2 goes against
the concept of object-oriented design. I think using this approach would
lead to applications that are difficult to understand and maintain.
 
C

C# Learner

C# Learner wrote:

Consider the following code.

<code>

static void Main()
{
Class c = new Class();
c.Method();
}

</code>

What really happens "behind the scenes" will be something along the
lines of:

<code>

static void Main()
{
Class c = new Class();

Of course, this would also compile down to something like:

Class c = Class_Construct();
 
J

Joe

So, would many instanciated classes have only ONE location in memory for
that method or MANY?
 
J

Joe

I agree with you guys. I prefer method #1. But suppose I need a million
objects in memory. Say the data members take up only 100 bytes per object.
And the methods take up another 1000 bytes of data. So in total, that's
100MB for data and 1GB for the methods. It seems a waste of memory to store
the 1GB of duplicate method data. Or does it need to be this way in order
to be thread safe?
 
J

Joe

"Joe" wrote...
It seems a waste of memory to store
the 1GB of duplicate method data.

Actually, how stupid of me! The method variables aren't static. They only
take up memory when those methods get used. The garbage collector frees
them up after they are out of scope of that method.

Thanks everyone, I needed to think this through.
 
J

Jon Skeet [C# MVP]

Joe said:
I have a fundamental question about classes (and structs for that matter).
Suppose I have a class with a bunch of members and methods. And this class
will be instantiated many many times at once in memory. Now, performance
wise and memory consumption wise, which method would be best to design the
class? My underlying concern is that method #1 will consume more memory
since each class instantiated will be larger than method #2. Is this a
false assumption?

Yes, it's a false assumption. How much memory an instance takes up only
depends on what members it has (and any attributes controlling its
layout). Having more methods will mean slightly more space usage in
total, but not on a "per instance" basis.
 
J

Jon Skeet [C# MVP]

The extra memory bagage for placing the methods in the same class is so
small that you would have to create MANY objects for you to have to be
anywhere close to concerned.

Why would it have any impact how many objects are created? The methods
only reside once in memory, however many instances there are.
 
C

C# Learner

Joe said:
So, would many instanciated classes have only ONE location in memory for
that method or MANY?

Only one, if it's the same method.

i.e.:

static void Main()
{
MyClass a = new MyClass();
MyClass b = new MyClass();

a.MyMethod(); // MyClass_MyMethod(a);
b.MyMethod(); // MyClass_MyMethod(b);
}
 
C

C# Learner

Joe said:
I agree with you guys. I prefer method #1. But suppose I need a million
objects in memory. Say the data members take up only 100 bytes per object.
And the methods take up another 1000 bytes of data. So in total, that's
100MB for data and 1GB for the methods. It seems a waste of memory to store
the 1GB of duplicate method data. [...]

That's not the case at all. A class's method body is *not* stored in
its instances.
 
A

Andreas Håkansson

Jon,

Erase my previous post from memory. This is the kind of brilliance I
sometimes
achive at 1am instead of shuting down the computer and taking a well earned
good
nights sleep ;)

//Andreas

"Jon Skeet [C# MVP]" <[email protected]> skrev i meddelandet
The extra memory bagage for placing the methods in the same class is so
small that you would have to create MANY objects for you to have to be
anywhere close to concerned.

Why would it have any impact how many objects are created? The methods
only reside once in memory, however many instances there are.
 
N

Nicholas Paldino [.NET/C# MVP]

Andreas,

I would think that what you are referring to is the slot in the vtable.
If you have an extra method, then the vtable would have an extra entry in
it, which consumes memory. However, this is a small cost. Also, to save
that slot, the only way you can get around it is to have your class call
static methods on another class, or call a method on an instance which is
shared among all the objects (if you have each instance store another,
separate instance of the calculation class, then you are actually wasting
more memory than if you placed the method on the class itself). However, I
would reserve this approach for only the most extreme cases.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Andreas Håkansson said:
Jon,

Erase my previous post from memory. This is the kind of brilliance I
sometimes
achive at 1am instead of shuting down the computer and taking a well earned
good
nights sleep ;)

//Andreas

"Jon Skeet [C# MVP]" <[email protected]> skrev i meddelandet
The extra memory bagage for placing the methods in the same class is so
small that you would have to create MANY objects for you to have to be
anywhere close to concerned.

Why would it have any impact how many objects are created? The methods
only reside once in memory, however many instances there are.
 
A

Andreas Håkansson

Nicholas,

As you say, the extra slot in the vTable is so small that you would have
to create massive amounts of objects befor this ever whould be a concern,
and besides if you create that amount of objects you would probably be
better of revising your design =)

//Andreas

Nicholas Paldino said:
Andreas,

I would think that what you are referring to is the slot in the vtable.
If you have an extra method, then the vtable would have an extra entry in
it, which consumes memory. However, this is a small cost. Also, to save
that slot, the only way you can get around it is to have your class call
static methods on another class, or call a method on an instance which is
shared among all the objects (if you have each instance store another,
separate instance of the calculation class, then you are actually wasting
more memory than if you placed the method on the class itself). However, I
would reserve this approach for only the most extreme cases.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Andreas Håkansson said:
Jon,

Erase my previous post from memory. This is the kind of brilliance I
sometimes
achive at 1am instead of shuting down the computer and taking a well earned
good
nights sleep ;)

//Andreas

"Jon Skeet [C# MVP]" <[email protected]> skrev i meddelandet
The extra memory bagage for placing the methods in the same class
is
so
small that you would have to create MANY objects for you to have to be
anywhere close to concerned.

Why would it have any impact how many objects are created? The methods
only reside once in memory, however many instances there are.
 
J

Jon Skeet [C# MVP]

As you say, the extra slot in the vTable is so small that you would have
to create massive amounts of objects befor this ever whould be a concern,
and besides if you create that amount of objects you would probably be
better of revising your design =)

But the point is that the number of instances of the class makes no
difference - there's only one vtable. You'd have to have massive
numbers of *classes* each with large numbers of methods to make a
difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top