6

I have a class in which I have a string field. This string field is constant and it will be moved in resource file in future but for the time being it will remain in our class. Now the scenario is that I am making more than hundred's of object of this class. So my question is in which approach less memory will be consumed and why?

  1. I should make my string variable static

    public class MyClass
    {
        public static string MyString = "My String";
    }
    
  2. I should make my string variable const

     public class MyClass
     {
        public const string MyString = "My String";
     }
    
MattDavey
  • 8,897
  • 3
  • 31
  • 54
Mohd Ahmed
  • 1,422
  • 13
  • 27
  • 3
    `Now the scenario is that I am making more than hundred's of object of this class.` But the `MyString` property will be allocated only once, no matter how many instance of the `MyClass` object you create. That's one of the points of using `static` – Kevin Gosse Jun 28 '13 at 09:55
  • Consider adding the `readonly` keyword if you're going for the first approach and see http://stackoverflow.com/questions/11323617/const-vs-readonly. – C.Evenhuis Jun 28 '13 at 09:56
  • 4
    _"So my question is in which approach less memory will be consumed and why?"_ - why do _you_ think it matters? Did you measure the difference? Or is this a hypothetical question? – CodeCaster Jun 28 '13 at 09:57
  • Related: [static readonly vs const](http://stackoverflow.com/q/755685/335858) – Sergey Kalinichenko Jun 28 '13 at 09:58

6 Answers6

5

There is a difference. Static variables are initialized in the dynamic memory space and therefore occupy additional memory space not to mention the fact that you need executable code (additional memory) to initialize them and access them.

All const data is located in the same memory space as executable code which is read-only-memory (ROM), where as static data is loaded into dynamic memory where you can potentially read and write to it. Now here is the interesting part that seems to have been overlooked in previous answers. Static data occupies dynamic memory space but requires additional ROM space and, in the worse case scenario, can take up twice the amount of memory. Consider the following:

class StaticData
{
    static string s_static1 = "My string data";
}

class ConstData
{
    const string CONST1 = "My string data";
}

The class StaticData has variable s_static1 which will occupy dynamic memory space of a single pointer (normally 1 integer). However it also has to be initialized and therefore there must also exist ROM code to initialize it. The string data itself would normally be located in ROM space, since string data is immutable, and would not take up any more space than the constant excample.

For the class ConstData there is only ROM storage space required and therefore this is, in most cases, the best usage of memory.

Now it gets more interesting when you consider how the data is used by the compiler. With the exception of string/character data, constants are normally substituted directly in code at the point of reference. In other words the constant value is directly loaded into a register or pushed onto the stack, depending on usage. In the case of a static variable the compiler has to read that value out of memory via additional code (pointer referencing) and therefore additional ROM code is required.

In summary static code will take up more memory both by occupying additional dynamic memory space and the additional executable code space required to de-referrence it.

Donald Fraser
  • 86
  • 1
  • 3
1

I believe that there won't be any difference with respect to memory. In both ways, memory consumed will be same. But there can be some difference in performance - constant field will be better since it seems to me you don't want value of MyString to be changed & a constant member is defined at compile time and cannot be changed at runtime.

Shumail
  • 3,103
  • 4
  • 28
  • 35
  • My bad - Edited answer - gave opinions on both - Memory & Performance a well – Shumail Jun 28 '13 at 10:30
  • 1
    What if I had `class MyClass { public static readonly string Used = "short string"; public static readonly string NeverUsed = "one gigabyte string here!"; }` As soon as I refer to `Used`, won't the runtime have to put the huge `NeverUsed` string into the heap? On the other hand, had `NeverUsed` been a `public const`, which was never really used, wouldn't I "save" some memory? – Jeppe Stig Nielsen Jun 28 '13 at 10:40
0

There is no differnce memory-wise. Either way you will load your string into memory once (if its value is not already there). And when it comes to strings, performance-wise there should not be much difference either as pointed out by KooKiz (there will be for value types, but normally you should not care about this kind of micro-optimisation)

Nikita B
  • 3,303
  • 1
  • 23
  • 41
0

Constant doesn't consume any memory. It will be replaced with defined value at compile time. While defining member as static will consume memory depending upon the data type.

0

Both options will use the same amount of memory. The only difference is that the first one will allow changing MyString's value and the second won't, which is probably what you want.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
-1

You should make it a global outside the class if it is an unchanged constant.

Otherwise, if it has to be part of the site make it static :)

ddoor
  • 5,819
  • 9
  • 34
  • 41