473,614 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instance Vs Static Methods and Memory Usage

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
Nov 16 '05 #1
4 7301
Inline ***
Willy.

"Mike" <mi********@com .nospam> wrote in message
news:u$******** ******@TK2MSFTN GP09.phx.gbl...
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.
Thanks,

Mike

Nov 16 '05 #2
Is there a good web resource on coding in C# for speed and efficiency?

Thanks,

Mike
Nov 16 '05 #3
"Mike" <mi********@com .nospam> wrote in
news:eu******** ******@TK2MSFTN GP11.phx.gbl:
Is there a good web resource on coding in C# for speed and
efficiency?


Mike,

http://msdn.microsoft.com/library

There are lots of articles under ".Net Development" and "Developmen t
Tools and Languages/Visual Studio .Net".

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #4
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.

--
Best regards
Mark Baldwin
Software Developer
"Mike" <mi********@com .nospam> wrote in message
news:u$******** ******@TK2MSFTN GP09.phx.gbl...
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
22506
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
2
3237
by: Rasika | last post by:
Memory usage wise is it a good idea to specify methods in utility/helper classes as instance methods rather than static methods? Rasika.
3
4209
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
2
1434
by: tconti | last post by:
Hello: We have an application that generates a static database layer based on stored procedures defined in SQL Server for consumption by client applications. These classes contain a slew of static methods. In many cases you only need to use 4 or 5 of these to retrieve/update data from the db. It seems like a collasal waste to have all of these excess static methods hanging around. When are these static methods loaded into memory? ...
22
1587
by: WXS | last post by:
Sometimes a method in a class requires the use of class instance variables/fields that will not be used outside of the method itself. Currently this means you must create a instance field in the class such that from a maintenance stand point it is disconnected from the method, and also affords the opportunity for other methods to mess with the variable when they never should. For example: public class MyClass
11
4308
by: syssyx | last post by:
I have a "CurrentUser" object in session that I want to access from each page of a vb.net website. I can successfully access everything if I include the following at the start of each pageload: Dim objCurrentUser As WebsiteClass.CurrentUser = Session("CurrentUser") If objCurrentUser Is Nothing Then objCurrentUser = New WebsiteClass.CurrentUser Any page can be linked to directly and not force a login, so without
3
1836
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is not ever instantiated (thus no constructor or destructor ever called), but instead, all of its member variables are defined as static in the according .cpp file, and it's methods are invoked by calling foo::bar(...) to invoke method "bar(...)".
4
4516
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
2
11691
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory leaking(thoug i konw i just donot have to).Then i look inside in time.h file(mingw) ,and i found notes say"These functions write to and return pointers to static buffers that may be overwritten by other function calls".So how is the"static buffers"...
0
8142
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5548
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4136
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.