Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How much memory do uninitialized array take up?

How much memory do uninitialized array take up?

Scheduled Pinned Locked Moved C#
data-structuresperformancequestion
11 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gigahertz205

    How much memory do uninitialized arrays take up? EX: int[] numbers; Do uninitialized value types take space too?

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    An uninitialized array is an object with all data set to zero/null, which can be served by one or more "demand zero" pages, i.e. it gets allocated in the page tables, but does not require physical memory pages yet. As soon as some of its data is needed (read or write) a page error will occur, and the MMU (Memory Management Unit, a hardware part of modern CPUs) will, with the help of some OS code, allocate physical memory for that page, and then fill it with zeroes. BTW the page size typically is 4KB Conclusion: an unitialized array is as expensive as a small object, no more, no less. Value types are: [ADDED] - either local types - or value members of some other type (such as an int inside a class object). Local value types are [/ADDED] allocated on the stack, so they consume memory corresponding to their size (although the stack too is virtual, i.e. if you allocate a huge struct, spanning many pages, most of these pages again would not be allocated immediately). There is no such thing as an uninitialized value type, every value type starts of with some value; typically the language definition and compiler enforce this; example: in a struct your constructor must assign a value to every member. :)

    Luc Pattyn [Forum Guidelines] [My Articles]


    Voting for dummies? No thanks. X|


    modified on Friday, July 4, 2008 10:41 PM

    N 1 Reply Last reply
    0
    • L Luc Pattyn

      An uninitialized array is an object with all data set to zero/null, which can be served by one or more "demand zero" pages, i.e. it gets allocated in the page tables, but does not require physical memory pages yet. As soon as some of its data is needed (read or write) a page error will occur, and the MMU (Memory Management Unit, a hardware part of modern CPUs) will, with the help of some OS code, allocate physical memory for that page, and then fill it with zeroes. BTW the page size typically is 4KB Conclusion: an unitialized array is as expensive as a small object, no more, no less. Value types are: [ADDED] - either local types - or value members of some other type (such as an int inside a class object). Local value types are [/ADDED] allocated on the stack, so they consume memory corresponding to their size (although the stack too is virtual, i.e. if you allocate a huge struct, spanning many pages, most of these pages again would not be allocated immediately). There is no such thing as an uninitialized value type, every value type starts of with some value; typically the language definition and compiler enforce this; example: in a struct your constructor must assign a value to every member. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      modified on Friday, July 4, 2008 10:41 PM

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #3

      Hello Luc, I agree with all what you said except "Value types are allocated on the stack". I believe it depends on the context where they are declared. Local variables goes to the stack, but instance variables will be on heap, AFAIK.

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      L 1 Reply Last reply
      0
      • N N a v a n e e t h

        Hello Luc, I agree with all what you said except "Value types are allocated on the stack". I believe it depends on the context where they are declared. Local variables goes to the stack, but instance variables will be on heap, AFAIK.

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        Hi N a v a n e e t h,

        N a v a n e e t h wrote:

        it depends on the context

        yes

        N a v a n e e t h wrote:

        Local variables goes to the stack, but instance variables will be on heap

        Not quite. There is no way to get a struct (or any other value type) directly allocated in the heap. Local variables are on the stack, value members of a type are located inside that type (the int inside a class). Remember: Rectangle rect; is allocating storage for a Rectangle struct; rect=new Rectangle(x,y,w,h); is not allocating anything, it is just assigning values to the members of rect. It is equivalent to rect.X=x; rect.Y=y; ... :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        N 1 Reply Last reply
        0
        • G gigahertz205

          How much memory do uninitialized arrays take up? EX: int[] numbers; Do uninitialized value types take space too?

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #5

          Array types are reference types even the element type is a value type. So int[] numbers would take 4KB of memory and will be kept on managed heap.

          gigahertz205 wrote:

          Do uninitialized value types take space too?

          Yes. There is nothing like uninitialized value type. Value types always will have a default value. So it takes space needed for keeping that value.

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          L G 2 Replies Last reply
          0
          • L Luc Pattyn

            Hi N a v a n e e t h,

            N a v a n e e t h wrote:

            it depends on the context

            yes

            N a v a n e e t h wrote:

            Local variables goes to the stack, but instance variables will be on heap

            Not quite. There is no way to get a struct (or any other value type) directly allocated in the heap. Local variables are on the stack, value members of a type are located inside that type (the int inside a class). Remember: Rectangle rect; is allocating storage for a Rectangle struct; rect=new Rectangle(x,y,w,h); is not allocating anything, it is just assigning values to the members of rect. It is equivalent to rect.X=x; rect.Y=y; ... :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Voting for dummies? No thanks. X|


            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #6

            Yeah - Thanks :)

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            1 Reply Last reply
            0
            • N N a v a n e e t h

              Array types are reference types even the element type is a value type. So int[] numbers would take 4KB of memory and will be kept on managed heap.

              gigahertz205 wrote:

              Do uninitialized value types take space too?

              Yes. There is nothing like uninitialized value type. Value types always will have a default value. So it takes space needed for keeping that value.

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #7

              N a v a n e e t h wrote:

              So it takes space needed for keeping that value.

              Address space sure, memory space only when the MMU page is actually needed. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Voting for dummies? No thanks. X|


              N 1 Reply Last reply
              0
              • L Luc Pattyn

                N a v a n e e t h wrote:

                So it takes space needed for keeping that value.

                Address space sure, memory space only when the MMU page is actually needed. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Voting for dummies? No thanks. X|


                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #8

                Luc Pattyn wrote:

                memory space only when the MMU page is actually needed.

                :doh: Yeah, you are correct. I forgot to mention. Thanks for mentioning. :)

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                1 Reply Last reply
                0
                • G gigahertz205

                  How much memory do uninitialized arrays take up? EX: int[] numbers; Do uninitialized value types take space too?

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #9

                  gigahertz205 wrote:

                  How much memory do uninitialized arrays take up? EX: int[] numbers;

                  It only takes up the memory used by the reference that you have declared. You haven't created the actual array yet.

                  gigahertz205 wrote:

                  Do uninitialized value types take space too?

                  They take up the same amount of memory regardless if they have a defined value or not. A value type that is a class member is always initialised. It's only when you have a value type as a local variable in a method that it can be undefined. It still has a value, but that value is undefined, so the compiler protects you from using the value before you have assigned anything to the variable.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    Array types are reference types even the element type is a value type. So int[] numbers would take 4KB of memory and will be kept on managed heap.

                    gigahertz205 wrote:

                    Do uninitialized value types take space too?

                    Yes. There is nothing like uninitialized value type. Value types always will have a default value. So it takes space needed for keeping that value.

                    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #10

                    N a v a n e e t h wrote:

                    So int[] numbers would take 4KB of memory and will be kept on managed heap.

                    No, it wouldn't. It's just a reference, so it will only take up four bytes (on a 32-bit system) wherever it's declared. If it's a local variable, it will take up four bytes of stack space.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    N 1 Reply Last reply
                    0
                    • G Guffa

                      N a v a n e e t h wrote:

                      So int[] numbers would take 4KB of memory and will be kept on managed heap.

                      No, it wouldn't. It's just a reference, so it will only take up four bytes (on a 32-bit system) wherever it's declared. If it's a local variable, it will take up four bytes of stack space.

                      Despite everything, the person most likely to be fooling you next is yourself.

                      N Offline
                      N Offline
                      N a v a n e e t h
                      wrote on last edited by
                      #11

                      Ohh it's 4bytes, not KB. Thanks Guffa for correcting. You rocks :)/

                      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups