473,498 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why is the stack faster?

hi all.
why value types which are stored in the stack, give better performance,
than reference types which are stored in the heap?
are not both stored in the ram?
thanks.
Nov 16 '05 #1
7 9460
n!
> why value types which are stored in the stack, give better performance,
than reference types which are stored in the heap?
are not both stored in the ram?


It is quicker to move the stack pointer than it is to allocate memory.
Allocating referenced objects is still fast in managed code, but changing
the stack pointer is still faster. Also taking into account that a method
knows which value types are created internally and thus they all get
allocated at once on entry to the method (or as part of their owning
object).

n!
Nov 16 '05 #2
n!,

Not quite true. The managed heap also uses a pointer to indicate were
the next object should be placed in the heap. Once the object has been
inserted into the heap, the pointer is moved to point after the last object.

However there is another layer of indirection since you will have to go from
stack -> heap before you get the correct object. Also the stack is local for
each thread and is inherintly thread safe, where as the heap is free-for-all
memory (of course for each application) so I would (wouldn't swear my
life on it - perhaps someone can confirm) think there is some kind of
lock mechanism being used to ensure thread safety of the information used
in the heap.

//Andreas

"n!" <nf********@nomailplease.com> skrev i meddelandet
news:O5**************@TK2MSFTNGP09.phx.gbl...
why value types which are stored in the stack, give better performance,
than reference types which are stored in the heap?
are not both stored in the ram?


It is quicker to move the stack pointer than it is to allocate memory.
Allocating referenced objects is still fast in managed code, but changing
the stack pointer is still faster. Also taking into account that a method
knows which value types are created internally and thus they all get
allocated at once on entry to the method (or as part of their owning
object).

n!

Nov 16 '05 #3
n!
> Not quite true. The managed heap also uses a pointer to indicate were
the next object should be placed in the heap. Once the object has been
inserted into the heap, the pointer is moved to point after the last object.
However there is another layer of indirection since you will have to go from stack -> heap before you get the correct object. Also the stack is local for each thread and is inherintly thread safe, where as the heap is free-for-all memory (of course for each application) so I would (wouldn't swear my
life on it - perhaps someone can confirm) think there is some kind of
lock mechanism being used to ensure thread safety of the information used
in the heap.


Yes, sorry. This is what I meant by 'allocating referenced objects is still
fast in managed code'. I just assumed there was 'a bit more' to it than
simply moving adjusting the pointer (and ignoring the GC collects that occur
infrequently) :) Unless I'm missing something else too?

Thanks,
n!
Nov 16 '05 #4
In fact, allocating memory in managed code can be faster than the
traditional unmanaged way. New objects will always be placed on top (or
bottom if you prefer) of the heap pointer, while the garbage collector
clears memory "holes", rearranges all objects and updates the reference
table pointers.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5
..NET heap allocation is very fast -- but this is usually irrelevant, since
most allocations cause garbage collection activity, and that is the big bad
bottleneck. Short and sweet article here with advice:

http://msdn.microsoft.com/library/en...asp?frame=true

BTW, heap objects are generally not thread safe until you make them thread
safe ... that's the whole point of e.g. lock():

http://msdn.microsoft.com/library/en...asp?frame=true

Brad Williams
"n!" <nf********@nomailplease.com> wrote in message
news:OG**************@TK2MSFTNGP12.phx.gbl...
Not quite true. The managed heap also uses a pointer to indicate were the next object should be placed in the heap. Once the object has been
inserted into the heap, the pointer is moved to point after the last object.

However there is another layer of indirection since you will have to go

from
stack -> heap before you get the correct object. Also the stack is local

for
each thread and is inherintly thread safe, where as the heap is

free-for-all
memory (of course for each application) so I would (wouldn't swear my
life on it - perhaps someone can confirm) think there is some kind of
lock mechanism being used to ensure thread safety of the information used in the heap.


Yes, sorry. This is what I meant by 'allocating referenced objects is

still fast in managed code'. I just assumed there was 'a bit more' to it than
simply moving adjusting the pointer (and ignoring the GC collects that occur infrequently) :) Unless I'm missing something else too?

Thanks,
n!

Nov 16 '05 #6
I would also add that the heap also incurs the possibility of page
faults whereas stack variables should always be in the current page.
Also, the compiler has a better optimization chance of storing values
in processor registers instead of RAM.
Nov 16 '05 #7
thanks for your help.
as i understand it now, the heap is slower because:
1. the extra way to reach the object.
2. garbage collection activity.
3. value types are compiled to work better with the processor registers.

true?

i also understand that i don't really understand what the stack is.
"Sharon" <ta*******@hotmail.com> wrote in message
news:en**************@tk2msftngp13.phx.gbl...
hi all.
why value types which are stored in the stack, give better performance,
than reference types which are stored in the heap?
are not both stored in the ram?
thanks.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
30065
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
0
1256
by: CoderGuy | last post by:
Hello I am reading up a bit on how memmory is used in the .NET Framework and have a few question about the stack-based approach * I understand that the stack is used to provide a layer of...
9
4946
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
17
5001
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
0
1158
by: CoderGuy | last post by:
Hello I am reading up a bit on how memmory is used in the .NET Framework and have a few question about the stack-based approach * I understand that the stack is used to provide a layer of...
24
2836
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
3
1890
by: Kirit Sælensminde | last post by:
I know that making new protected or private will (generally) prevent instances from being created on the heap, but I was wondering about preventing them on the stack. I saw in another post a...
16
4416
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
52
5047
by: Nomad.C | last post by:
Hi I've been thinking of learning Fortran as number crunching kinda language for my Physics degree......but then looking around the internet, people are saying that the libraries/ Algorithms once...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.