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.
 
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.
 
Back
Top