Re: stack and a heap
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Mon, 6 Jun 2005 11:33:13 +0200
"Gino Cerone" <cosine@xxxxxxxxx> wrote in message
news:u%23P6q8iaFHA.796@xxxxxxxxxxxxxxxxxxxxxxx
> When an application is opened Windows allocates the full 32 bit address
> space (4 gigs) of memory. (this is theoretical and Windows memory manager
> maps from the actual memory to the 32 bit memory space) It sub-divides
> this
> into two parts, the Stack and Heap. The stack holds the declared named
> varibles. In the case of a primative data type like 'byte' it holds the
> value,
>
> byte mybyte;
> mybyte = 125;
>
> In the case of instantiating a class, the declared name varible is writen
> to
> the stack. The instaniated Object in writen to the Heap. The value of the
> varible in the stack holds the address space of the Object in the Heap.
>
> sqlconnection myconnection; (declares the varible myconnection of type
> sqlconnection in the stack. If you tried to use myconnection
> now you would get a null
> reference error because there isn't a memory address in the value yet.)
>
> myconnection = new sqlconnection; (this line instantiates the class
> 'sqlconnection' into an Object in the Heap, and enters the address
> of the Object in
> the value for myconnection. myconnection becomes the pointer that
> references
> the Object
> 'sqlconnection' in the Heap.)
>
>
> "shine" <shine@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:5D2373B4-7795-4B56-9008-E97E74A54690@xxxxxxxxxxxxxxxx
>> what is the difference between a heap and a stack?
>
>
Sorry, but you present a very simplistic view of the real process memory
space and you got it wrong on several points.
1. The full 32 bit address space is not allocated as user process space,
only a maximum of 2GB or 3GB (using /3GB RAM tuning switch in boot.ini) can
ever be reserved (and possibly committed).
2. This space is not sub-divided into a stack and a heap, there is stack
space allocated per thread created and there is heap space. When the OS
creates a process it always starts with a single thread so your process
always start with a single stack (default size: 4KB committed, 1MB reserved
space).
The default sizes of heap and stack space is determined by:
- the built process - managed code generators generate defaults (see later),
using unmanaged code build tools (like link.exe) one can specify other
values for both heap and stack.
- the values assigned by tools like editbin....
The default sizes for ".NET" processes is:
for the heap 4KB committed and 1MB reserved, extra space can (and will)
be requested by the process at run time.
for the stack 4KB committed and 1MB reserved, the stack can never grow
beyond the reserved maximum.
3. The stack only holds local variables, not all "the declared named
variables".
4. Primitive types (as all value types) are only stack allocated when
declared local (they have function scope), instance variables are GC heap
allocated. Note that locals can also be register allocated!
5. This:
> sqlconnection myconnection; (declares the varible myconnection of type
> sqlconnection in the stack. If you tried to use myconnection
> now you would get a null
> reference error because there isn't a memory address in the value yet.)
is not completely true, the compiler will not allow you to use an
uninitialized local variable. If he would allow it, the variable could hold
any possible value that was left on the stack at the location of the
variable.
Willy.
.
- Follow-Ups:
- Re: stack and a heap
- From: Gino Cerone
- Re: stack and a heap
- References:
- stack and a heap
- From: shine
- Re: stack and a heap
- From: Gino Cerone
- stack and a heap
- Prev by Date: Re: Images in a combobox
- Next by Date: Re: Threads so darn hard to understand
- Previous by thread: Re: stack and a heap
- Next by thread: Re: stack and a heap
- Index(es):
Relevant Pages
|