locked
Use of const and static to hold global variables RRS feed

  • Question

  • Hey. I am currently working on a project that relies heavily on using const and static variables to store values to be accessed by the entire program. I was wondering how this can affect performance and memory usage and if there is a better way to do it?

    Here is some more background.

    There is a static class that contains only static variables. The variables are most of int or string types. They hold keys that are used throughout my program to map values. There are over 200 static defined variables.

    I also have another class that has both const and static fields that number in the hundreds.

    Would there be a benefit to use const instead of static? Are both types loaded into memory once the application is running?
    Wednesday, July 16, 2008 12:56 PM

Answers

  • The main difference here is that const is limited to simple values, and is burned directly into the caller (it is never read by a caller at runtime), where-as a static variable can be set at runtime (only in the .cctor if static, for example from a config file), and can have comlex values (for example, a reference to a custom type).

    So really:
    • is is a fixed absolute (like "Pi")? consider a const
    • is it determined from config? consider a static readonly variable (exposed by a property)
    • does it change at runtime? consider a static mutable variable (exposed by a property than handles thread synchronization)

    In terms of memory; for simple values I simply wouldn't worry about it. The bigger risk is accidentally keeping lots of objects alive, by accidentally keeping their references available from the static variable (for instance, static events are notorious for this risk). This is something you simply need to watch for and design around...


    Marc
    • Marked as answer by brownman98 Wednesday, July 16, 2008 2:48 PM
    Wednesday, July 16, 2008 1:27 PM

All replies

  • The main difference here is that const is limited to simple values, and is burned directly into the caller (it is never read by a caller at runtime), where-as a static variable can be set at runtime (only in the .cctor if static, for example from a config file), and can have comlex values (for example, a reference to a custom type).

    So really:
    • is is a fixed absolute (like "Pi")? consider a const
    • is it determined from config? consider a static readonly variable (exposed by a property)
    • does it change at runtime? consider a static mutable variable (exposed by a property than handles thread synchronization)

    In terms of memory; for simple values I simply wouldn't worry about it. The bigger risk is accidentally keeping lots of objects alive, by accidentally keeping their references available from the static variable (for instance, static events are notorious for this risk). This is something you simply need to watch for and design around...


    Marc
    • Marked as answer by brownman98 Wednesday, July 16, 2008 2:48 PM
    Wednesday, July 16, 2008 1:27 PM
  • Thanks. That helps.
    Wednesday, July 16, 2008 2:47 PM