Question

I mean, does something, for example in C#, like

String test = "Test";
txtCtrl.Text = test;

consumes more RAM than just

txtCtrl.Text = "Test";

?

As it's said, a variable is a reserved space in memory. But does that mean that not using a variable won't "reserve" (consume, take) a space in memory?

Was it helpful?

Solution

Maybe.

Depending on your language, and how you use it, and how smart your compiler is, it may or may not take more memory. And even if it does take more memory, it's likely to take less memory (just memory for the reference to the data, rather than the entirety of the data itself).

In the long run, the memory usage is going to be inconsequential to the memory usage of your program, whereas the use of local variables will usually have a significant impact on the readability of your program.

Don't worry about it.

OTHER TIPS

Probably.

Because you're assigning the same string to both containers, you're essentially assigning the same string reference. So the string is only stored in memory once, but the reference is stored in two places.

To put it another way, string test holds a reference to a string object. The size of that reference is 4 bytes in a 32 bit CIL environment, and 8 bytes in a 64 bit one. The string itself is however long it is; each Unicode character consumes two bytes, but there is only one string here.

txtCtrl.Text also holds a reference, pointing to the same string object.

Licensed under: CC-BY-SA with attribution
scroll top