How much memory do uninitialized array take up?
-
How much memory do uninitialized arrays take up? EX: int[] numbers; Do uninitialized value types take space too?
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
-
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
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
-
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
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 torect.X=x; rect.Y=y; ...
:)Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
How much memory do uninitialized arrays take up? EX: int[] numbers; Do uninitialized value types take space too?
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
-
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 torect.X=x; rect.Y=y; ...
:)Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
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
-
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
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 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|
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
-
How much memory do uninitialized arrays take up? EX: int[] numbers; Do uninitialized value types take space too?
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.
-
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
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 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.
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