4

Say we have a class with 10000 const string members.

class Schema
{
  //Average string length is 20
  public const string ID1 = "some.constant.value";
  public const string ID2 = "some.other.constant.value";
  //...
}

Not all fields are referenced in the rest of the code. Only 10% of them is accessed on startup - their reference is assigned as a key to various dictionaries (thousands of Dictionary instances). I understand that const strings are interned - referencing a const string multiple times does not increase consumed memory by more than the size of the metadata token pointing to an offset in the interned string table.

I understand that the const strings are compiled into the assembly and thus influence the size of the compiled assembly.


At what exact time/event do these const strings consume runtime memory?

Will be all the memory needed for all the const strings taken at the time the assembly is loaded or is this delayed until the class is JIT compiled?

Can we decrease memory consumption after startup by changing something in the equation? (make the fields non-const, make the strings static fields?).

Let's assume a Winforms application (.NET 2.0).

Marek
  • 10,307
  • 8
  • 70
  • 106
  • 2
    As a rough guesstimate, the total memory consumption will be on the order of 300 kB, which is about 0.0075% of a modern computer's RAM (4 GB). How important is this? – Marcelo Cantos Jul 21 '10 at 11:42
  • 1
    Forget the figures, memory usage optimization is not the goal here. This question is asked mainly for the sake of understanding the principles. – Marek Jul 21 '10 at 11:45
  • @Marcelo: what does RAM have to do with it? RAM is a performance optimization; the scare resource isn't RAM, it's virtual address space. When you "run out of memory" you've run out of address space, not RAM; if you run out of RAM then the operating system just goes to disk and pages it. It's not 1980 anymore; we have virtual memory. :-) – Eric Lippert Jul 21 '10 at 14:23
  • 1
    if you want to understand how memory is consumed in .NET I recommend running some apps under the memory profiler, and examine its output. You can learn a lot about what's going on in your application. As for when string constants are created: I simply wouldn't worry about it. It's an implementation detail, and the total memory involved is usually small. – Eric Lippert Jul 21 '10 at 14:46
  • @Eric: The same argument applies for RAM as a performance optimisation. Just as we should fret about how we utilise L1/L2/L3 cache, we should also fret about our use of physical RAM. Sure, if most of those constants are never touched, they will only consume address space, but if they are used continually, then they will also consume physical RAM and increase the pressure on the paging system. In short, both RAM and address space are important considerations, even in the year 2010. – Marcelo Cantos Jul 21 '10 at 23:00

3 Answers3

3

Const strings are compile time literals, and since the CLR uses interning for these they will stick around for as long as the application is alive.

You may also find my answer to this question relevant.

Community
  • 1
  • 1
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
0

I may be wrong but when the assembly is first referenced it is loaded entirely in memory with all the code, metadata and constant values (i don't know for sure if embedded resources are loaded also or deferred). And it will remain loaded until the process is terminated

Adrian Zanescu
  • 7,907
  • 6
  • 35
  • 53
0

It doesn't matter that the strings are constants. The constants themselves doesn't take up any memory at all, it's the literal strings that take up memory.

When you use the constants in your code, the compiler will substitute it for a reference to the literal string.

The string literals are loaded with the assembly, so they stick around throughout the life time of the application. Even if you change your constants to something else, the string literals are still there. Using variables instead of constants will actually use more memory, because it needs somewhere to store the value of the variable (i.e. a copy of the reference to the literal string).

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • So you are suggesting that if I do not use any of the constants in the code, no memory will be taken by them? – Marek Jul 21 '10 at 11:49
  • @Marek: The literal strings will still take up memory, the compiler can't optimise them away. The constants (i.e. references to the literal strings) are declarations used at compile time, they never take up any memory. – Guffa Jul 21 '10 at 11:53