Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

stack/heap question

Is there a limit to the amount of memory (besides the obvious physical one) on the stack or heap? I have a fairly large global data structure and I'd like to know if it would make a difference whether it is allocated on the stack or on the heap.
0
VEngineer
Asked:
VEngineer
  • 4
  • 4
  • 2
1 Solution
 
VEngineerAuthor Commented:
Edited text of question
0
 
trestanCommented:
Automatic created variable will be stored at stack. The dynamicely created variable will be in heap. When a variable is created in heap, an additional space will also be occupied to store the related imformation.
0
 
trestanCommented:
If your data structure is global, it is better to be put in stack. You can try to detect the available space by using new.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nietodCommented:
>> is there a limit to the amount of memory (besides the obvious physical one) on the stack or heap?
Yes.  But it depends on the implimentation you ae running on.  

For example,  In a DOS application you stack will be limited to 64K but will probably be much smaller.  Your heap could be larger than 64 K depending on you memory model.  
In a 32 bit windows program your stack and heap can both be larger (and likely will be) than 64 K.  Under windows the size is a lot less limited because virtual memory can be used to allow the stack and heap to expand (not indefinitly).

>> I  have a fairly large global data structure and I'd like to know if it would make a difference whether it is allocated on the stack or on the heap.

Probably better on the heap.  Heaps tend to be larger (considerably) and are expected to grow more.
0
 
nietodCommented:
>> When a variable is created in heap, an additional space will also be occupied to store the related imformation.

But for a single large object this isn't significant.  The additional information is proably a pointer and a size, say 8 bytes.  If the object in question is 8K, we are talking about a 0.1% increase in memory consumption.  That is not a big deal.  Now if you are lots of small objects, then this exta information can get significant.

>> You can try to detect the available space by using new.
That's not a good idea these days (used to be fine).  On an implimentation that supports virtual memory, this can cause your heap to grow to huge sizes before new fails.  This can have a drastic impact on performance of your application and others.
0
 
trestanCommented:
Another consideration is whether you want to consume more time on compiling or on running. Using new will considerably increase the running time. It depends on the purpose of the software. If you develop the program and other people use it, it is prefered to spend more time on compiling and the client will not wait long time when it runs. You can try both. I do not see the much difference between the size of the stack and the size of the heap. Although some books say that it is better to create large object on heap, but my former programming, it seems that either the stack or the heap may use up the memory of the same limit.  
0
 
nietodCommented:
>> Using new will considerably increase the running time

Dynamic allocations do take a lot longer than allocating on the stack.  But on a modern computer, you can do thousands of dynamic allocations in a second.   So if we are talking about one or two objects, it isn't relevant.  
0
 
trestanCommented:
Just try to allocate a Array[10000000], you will see how long it will take. I think the memory management concept is different before and after window95 and whether it is a pentium machine.
0
 
nietodCommented:
Allocating a huge array doesn't take really longer than allocating a small array.  The time spent in allocating is the time required to search the heap for a free block (which is not done on the stack).  For a larger block you may have a longer search until you find a block big enough, but not much longer.   You can still search a typical heap thousands of times a second.  Thus if you are allocating one or two huge arrays, you will not see a performance difference between allocating them on the stack or allocating them on the heap.  The difference will be a matter of milliseconds.  If you were allocating thousands of these arrays, then you would notice a difference.
0
 
VEngineerAuthor Commented:
This discussion helps a lot.  I'll think about my design more and keep these issues in consideration.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now