Will instantiating a lot of variables (string, int, etc.) consume a lot of memory?

Will instantiating a lot of variables (string, int, etc.) consume a lot of memory?



I've been thinking of making my code more readable. I have a lot of foreach and for loops since I handle lots of DataTables in my code.



Is it okay if I instantiate column names that I will use inside loops? Or will it consume too much memory? For example:


DataTable fooTable = new DataTable();

foreach(DataRow dr in fooTable.Rows)

string nameColumn = dr.Field<string>("NAME")
Console.WriteLine(nameColumn)



vs.


DataTable fooTable = new DataTable();

foreach(DataRow dr in fooTable.Rows)

Console.WriteLine(dr.Field<string>("NAME"))





Memory is allocated when you instantiate an object, not when you reference it. Thus no matter how many references to the same instance you have, the memory won´t increase (apart from the really low memory-consumption of 4bytes (???) for the reference itself).
– HimBromBeere
Aug 31 at 10:17






So is it advisable if I instantiate a lot of objects, for the purpose of having a clearer and more readable code?
– thecodeexplorer
Aug 31 at 10:20





Don't guess with memory performance. Set (realistic) goals, write simple, easy to read code, then measure. If it meets your goals, great, move on. Admittedly, it takes some experience to know what realistic goals are, but don't try to learn anything performance related by memorizing rote rules.
– Damien_The_Unbeliever
Aug 31 at 10:20





I guess I'll worry about memory and performance later. For now, I'll stick with what you've advised and make the code more readable, even if I end up using lots of variables.
– thecodeexplorer
Aug 31 at 10:23





Rule number one of optimization: you're usually wrong about where you think performance problems might be. Point in case: DataTable is a very inefficient class in terms of memory use, compared to containers of strongly-typed objects. If memory use truly was an issue, that would likely be worth looking at way sooner than any of your variables. (Rule zero also applies here: you're usually wrong about the need to optimize in the first place, and you're always wrong if you haven't measured anything yet.)
– Jeroen Mostert
Aug 31 at 10:27



DataTable




3 Answers
3



It's likely (certainly in debug mode) that there's an unnamed local variable used to store the result of dr.Field<string>("Name") in the version where you don't name a variable.


dr.Field<string>("Name")



And even then, the variable exists once in this method. You don't get a new variable each time through the loop.



Memory is allocated when you instantiate an object, not when you reference it. Thus no matter how many references to the same instance you have, the memory won´t increase.



This means the following two lines are identical considering their memory-footprint:


MyClass a = new MyClass();
Console.WriteLine(a);



and


Console.WrileLine(new MyClass());



in both cases there´s exactly one single instance of your class, not two. Thus the memory-consumoption is exactly that for one instance of the class (plus some small amount for the references theirselves, on a 32bit machine that´s 4bytes, on 64bit 8byte).



In your case memory is allocated when calling dr.Field<string>("NAME"). However in your first code the object is referenced, thus it may be garbage-collected some nano-seconds later (when the loop is on the next iteration).


dr.Field<string>("NAME")



Having said this it´s very unlikely that your memory increases much (if at all) when you have a million references all referencing the exact same object.



You should definitely use a temporary variable like nameColumn whenever you are in this kind of situation.


nameColumn



Each time you call dr.Field<string>("NAME"), the runtime has to construct an iterator and execute a string matching algorithm on a collection of objects just to find the right field to access. The amount of overhead caused by accessing the Field method far exceeds whatever impact may be caused by the temporary variable.


dr.Field<string>("NAME")


Field



So if you ever plan to use nameColumn in more than one place, the variable is what you need. Even if you don't re-use it, putting it in at the beginning is not a bad practice.


nameColumn



Adding the variable is not likely to have any negative impact. The compiler may even end up generating the same IL anyway.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)