Instance Vs Static Methods and Memory Usage

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

This question has been in the back of my head for some time, so I'll ask.

If I have a method that has 200 Lines of code and I create 1000 instances of
the class to which the method belongs, are 1000 copies of the compiled code
in that method created in memory or is only the raw data for each of the
1000 classes stored in memory?

Does this also apply to the code in properies with get set acessors?

How does a static member or property differ concerning memory usage in the
above example?

Thanks,

Mike
 
Inline ***
Willy.

Mike said:
This question has been in the back of my head for some time, so I'll ask.

If I have a method that has 200 Lines of code and I create 1000 instances
of
the class to which the method belongs, are 1000 copies of the compiled
code
in that method created in memory or is only the raw data for each of the
1000 classes stored in memory?
**** One instance of the compiled code (code members), 1000 instances of the
object data.
Does this also apply to the code in properies with get set acessors?
*** Yep.
How does a static member or property differ concerning memory usage in the
above example?
*** One instance of data and code.
 
Is there a good web resource on coding in C# for speed and efficiency?

Thanks,

Mike
 
When the class is instantiated, memory is allocated for the data members
only. The "this" pointer points to the this data so the code only exists in
one place but operates with different "this" pointers for each instance.

For static members, there is only 1 instance that is shared across all
instances of the class that it is declared in.
 

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

Back
Top