473,468 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Create 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 9456
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
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
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
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
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
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
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
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
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.