none
Usage of Static members. RRS feed

  • Question

  • Hi all,
           I need to know whether using a static member will increase the usage of memory space or slows down the execution of a program?

    Thank you all.
    Tuesday, January 6, 2009 6:31 AM

Answers

  • A static field is a allocated in the appdomain's static memory heap by the JIT compiler.  There will ever only be one allocation for it.  The JIT compiler generates a direct load for the value, using the memory address.  A non-static field is allocated on the heap (assuming it isn't in a struct).  It takes just as much space as the static, but there will be as many allocations for it as there are objects.  The JIT compiler must use an indirect load for the value, using the object reference.

    Long story short: a static is almost always more efficient.  But it also will always be around, using memory for the lifetime of the program.

    Hans Passant.
    • Marked as answer by nobugzModerator Saturday, January 10, 2009 9:00 PM
    • Unmarked as answer by beesbharath Monday, January 12, 2009 5:07 AM
    • Unmarked as answer by beesbharath Monday, January 12, 2009 5:07 AM
    • Marked as answer by Zhi-Xin Ye Monday, January 12, 2009 11:34 AM
    Tuesday, January 6, 2009 1:21 PM
    Moderator

All replies

  • Hello,

    That depends of course of what you are going to reference in your static field. I have some project with huge numbers of (small) static member, and haven't notice any drawback.

    The one thing I know that bloat the memory in the framework is related to hidden caches (stored in static variable ;-) use for reflection (if you use Reflection very very extensively, you will notice it).
    Tuesday, January 6, 2009 10:00 AM
  • A static field is a allocated in the appdomain's static memory heap by the JIT compiler.  There will ever only be one allocation for it.  The JIT compiler generates a direct load for the value, using the memory address.  A non-static field is allocated on the heap (assuming it isn't in a struct).  It takes just as much space as the static, but there will be as many allocations for it as there are objects.  The JIT compiler must use an indirect load for the value, using the object reference.

    Long story short: a static is almost always more efficient.  But it also will always be around, using memory for the lifetime of the program.

    Hans Passant.
    • Marked as answer by nobugzModerator Saturday, January 10, 2009 9:00 PM
    • Unmarked as answer by beesbharath Monday, January 12, 2009 5:07 AM
    • Unmarked as answer by beesbharath Monday, January 12, 2009 5:07 AM
    • Marked as answer by Zhi-Xin Ye Monday, January 12, 2009 11:34 AM
    Tuesday, January 6, 2009 1:21 PM
    Moderator
  • HI Nobugz,
                  what do you mean by appdomain's static memory heap. Also tell me the way the compiler allocates memory for different kinds of variables.
    Monday, January 12, 2009 5:13 AM
  • I mean the heap that is used to store static members.  You'll have to start a new thread for your new question.
    Hans Passant.
    Monday, January 12, 2009 9:36 AM
    Moderator