Hello all,
I am writing a program which needs to run as quickly as possible, but holds
a lot of data in memory (around 1GB for a usual run). Is this too much
memory to even consider putting most/all of it on the stack? Does it
matter?
Any considerations I might take into account when deciding how to allocate
the many data structures? By the way, the system will consist of a handful
of very large data structures (primarily matrices and vectors).
Historically, I have always used the heap. This was more a result of my C
background than anything else. However, I am now writing a system which
will use only C++ and OO practices. Therefore, I feel I have a new choice
to make. Amy assistance to make this design as efficient as possible would
be greatly appreciated.
Thank you,
dan elliott 11 2274
Dan Elliott napisał: Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack? Does it matter? Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices and vectors). Historically, I have always used the heap. This was more a result of my C background than anything else. However, I am now writing a system which will use only C++ and OO practices. Therefore, I feel I have a new choice to make. Amy assistance to make this design as efficient as possible would be greatly appreciated.
C++ has no concept of heap or stack (well, not in the meaning you
have used, anyway), but <OT> usually the data allocated via
new or malloc will end up on the heap. The stack is used for
local objects which should not be too large. The stack is
usually limited to at most a few MB, often less. </OT>
Therefore I suggest you allocate from the heap:
void foo() {
bigmatrix Matrices[1000]; // allocates on the stack, no-go
}
vod goo() {
bigmatrix* Matrices =new bigmatrix[1000]; // allocates on the heap, ok
// don't forget to "delete[] Matrices", when you're done
}
I suspect you have a custom matrix or vector class? If not,
you might want to use std::vector<> for your data.
HTH,
- J.
Dan Elliott wrote: I am writing a program which needs to run as quickly as possible but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack?
How would you "put it on the stack"?
Does it matter?
Use automatic storage [the stack] if you have lots of small objects.
Using automatic storage for large objects [> 1GByte]
might not be such a good idea.
Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices and vectors).
Unless you are planning to use variable size arrays
to represent vectors and matrices, you will be allocating memory
from free storage for these objects anyway.
Historically, I have always used the heap. This was more a result of my C background than anything else. However, I am now writing a system which will use only C++ and OO practices. Therefore, I feel I have a new choice to make.
I don't think so.
I think that you are confused.
All of the vector and matrix classes for large objects
allocate memory from free storage.
Any assistance to make this design as efficient as possible would be greatly appreciated.
Take a look at
The Scalar, Vector, Matrix and Tensor class Library http://www.netwood.net/~edwin/svmtl/
then visit
The Object-Oriented Numerics Page http://www.oonumerics.org/oon/
On Wed, 17 Nov 2004 22:02:58 +0100, Jacek Dziedzic wrote: Dan Elliott Hello all,
I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack? Does it matter?
Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices and vectors).
Historically, I have always used the heap. This was more a result of my C background than anything else. However, I am now writing a system which will use only C++ and OO practices. Therefore, I feel I have a new choice to make. Amy assistance to make this design as efficient as possible would be greatly appreciated.
C++ has no concept of heap or stack (well, not in the meaning you have used, anyway), but <OT> usually the data allocated via new or malloc will end up on the heap. The stack is used for local objects which should not be too large. The stack is usually limited to at most a few MB, often less. </OT>
Therefore I suggest you allocate from the heap:
void foo() { bigmatrix Matrices[1000]; // allocates on the stack, no-go }
vod goo() { bigmatrix* Matrices =new bigmatrix[1000]; // allocates on the heap, ok
// don't forget to "delete[] Matrices", when you're done }
I suspect you have a custom matrix or vector class? If not, you might want to use std::vector<> for your data.
HTH, - J.
Thank you for the response. So, as a general rule, large objects should
be allocated on the "heap?" In what way is the "stack" limited in size
that the "heap" is not? I sorry if this is something I should have
learned as an undergrad.
This question became especially salient in my mind when reading about the
uBLAS library. They mention that a particular data structure will
allocate matrices on automatic storage (stack in my terminology) which
is better than free store.
Thanks again for making my stay in system design hell a little brighter.
- dan
On Wed, 17 Nov 2004 13:03:24 -0800, E. Robert Tisdale wrote: Dan Elliott wrote: I am writing a program which needs to run as quickly as possible but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack?
How would you "put it on the stack"?
Does it matter?
Use automatic storage [the stack] if you have lots of small objects. Using automatic storage for large objects [> 1GByte] might not be such a good idea.
Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices and vectors).
Unless you are planning to use variable size arrays to represent vectors and matrices, you will be allocating memory from free storage for these objects anyway.
Historically, I have always used the heap. This was more a result of my C background than anything else. However, I am now writing a system which will use only C++ and OO practices. Therefore, I feel I have a new choice to make.
I don't think so. I think that you are confused. All of the vector and matrix classes for large objects allocate memory from free storage.
Any assistance to make this design as efficient as possible would be greatly appreciated.
Take a look at The Scalar, Vector, Matrix and Tensor class Library
http://www.netwood.net/~edwin/svmtl/
then visit The Object-Oriented Numerics Page
http://www.oonumerics.org/oon/
Thank you for the information. I am planning to use uBLAS which can do
either free or automatic storage.
None of my individual vector or matrices will occupy more than, perhaps,
50MB. However, all told, the vectors and matrices could easily total over
1GB of data.
Do you still suggest that I use the heap exclusively. Would it be
sensable to use the automatic storage whenever advisable (still not sure
when that would be)?
Is access to free-store slower? What are the incentives for using
automatic storage?
Thank you, everyone, for your time.
- dan elliott
Daniel L Elliott wrote: E. Robert Tisdale wrote:
Dan Elliott wrote:
I am writing a program which needs to run as quickly as possible but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack?
How would you "put it on the stack"?
Does it matter?
Use automatic storage [the stack] if you have lots of small objects. Using automatic storage for large objects [> 1GByte] might not be such a good idea.
Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices and vectors).
Unless you are planning to use variable size arrays to represent vectors and matrices, you will be allocating memory from free storage for these objects anyway.
Historically, I have always used the heap. This was more a result of my C background than anything else. However, I am now writing a system which will use only C++ and OO practices. Therefore, I feel I have a new choice to make.
I don't think so. I think that you are confused. All of the vector and matrix classes for large objects allocate memory from free storage.
Any assistance to make this design as efficient as possible would be greatly appreciated.
Take a look at The Scalar, Vector, Matrix and Tensor class Library
http://www.netwood.net/~edwin/svmtl/
then visit The Object-Oriented Numerics Page
http://www.oonumerics.org/oon/
Thank you for the information. I am planning to use uBLAS which can do either free or automatic storage.
None of my individual vector or matrices will occupy more than, perhaps, 50MB. However, all told, the vectors and matrices could easily total over 1GB of data.
Do you still suggest that I use the heap exclusively. Would it be sensable to use the automatic storage whenever advisable (still not sure when that would be)?
Is access to free-store slower? What are the incentives for using automatic storage?
Thank you, everyone, for your time.
Take a look at the TinyVector and TinyMatrix classes in Blitz++ http://www.oonumerics.org/blitz/examples/
Object allocation on Heap or Stack have nothing much to do with C or
C++. It is more of a design issue independent of the language.
There are a few things which you need to consider while choosing the
option. Object creation and deletion on stack is faster than that on
Heap; but once the objects are created there is no difference in access
times required for objects on stack or on heap.
Stack is there for automatic variables and its size is limited. So, I
suggest that you use heap for your data storage. If have performace to
consider then see to it that object creation and deletion might take
some time and you should minimize on that; like instead of allocation
10 small memory chunks, you can create a larger chunk of memory and
then divide it by urself.
Also, I'd suggest you not to use variable sized arrays as they are
implemented in libraries using lists or something and access to them is
definetly slower than the fixed sized conventional arrays.
Also maybe u can have a look at the GNU Scientific library as an option
for components. www.gnu.org/software/gsl/
Prash wrote: Stack is there for automatic variables and its size is limited. So, I suggest that you use heap for your data storage.
Actually, automatic storage [the program stack]
and free storage [the heap] share the same virtual memory.
The typical program stack grows up from the bottom of virtual memory
and the heap grows downward to the top of the stack.
The stack size may be limited but can be easily increased
using shell commands or compiler options.
For example, on a Linux workstation:
limit stacksize
stacksize 10240 kbytes limit stacksize unlimited limit stacksize
stacksize unlimited
If [you] have performace to consider, then see to it that some time object creation and deletion might take and you should minimize on that; like, instead of allocation 10 small memory chunks, you can create a larger chunk of memory and then divide it by yourself.
Also, I'd suggest you not to use variable sized arrays as they are implemented in libraries using lists or something and access to them is definetly slower than the fixed sized conventional arrays.
No!
C99 style variable size arrays http://gcc.gnu.org/ml/gcc/2004-05/msg00746.html
are allocated from free storage [the stack]
and have performance characteristics similar to "conventional arrays".
Also, maybe you can have a look at the GNU Scientific library as an option for components. www.gnu.org/software/gsl/
You may as well check out
the Vector, Signal and Image Processing Library http://www.vsipl.org/
or, better yet,
the High Performance Embedded Computing Software Initiative http://www.hpec-si.org/
which actually proposes a C++ language binding.
"Dan Elliott" <da************************@noSpam.org> wrote in message news:<8M***********@dfw-service2.ext.ray.com>... Hello all,
I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack? Does it matter?
I assume with that with 'stack' you mean allocations like:
void foo () {
int x[BIG];
}
and with heap you mean
void bar () {
int* ax = new int[BIG];
delete[] ax;
}
In that case, the answer is that you should use the latter.
However, in C++ the issue you probably want
void foobar() {
std::deque<int> x(BIG);
}
if you could use both. You don't have to clean up, and there are
less size limits. In fact, std::deque is likely to support larger
arrays than new.
If you pass them around, be aware that copying a std::deque is rather
expensive. In such cases a boost::shared_array class might help
( www.boost.org)
Regards,
Michiel Salters
Daniel L Elliott wrote: In what way is the "stack" limited in size that the "heap" is not? I sorry if this is something I should have learned as an undergrad.
As far as I understand it, upon the start of your program a fixed
amount of memory is denoted as 'stack' and the rest of the memory
is denoted as heap. In that way, stack is usually limited to a
certain amount -- often only 1MB in order not to waste too much
memory that could be used as heap (because you can't shrink the
stack if it is not used).
That way you get a limited amount of stack storage. This can
bite you in a cruel manner if you don't remember about it.
Under Linux, when the stack is overfilled you usually only
get "Aborted" and your program dies instantly.
The size of the stack is usually controlled via a compiler
switch.
You might try to run these two programs:
// program 1
// This tries to have a 50MB structure on stack
// Will most likely crash
void foo() {
char bigarray[50000000];
bigarray[0]=0; // use bigarray to make sure the
// compiler does not optimize it away
}
int main() {
foo();
}
// program 2
// This tries to allocate a 50MB structure on the heap
// Will most likely succeed, if you have 50MB spare memory
#include<iostream>
void foo() {
try{
char* bigarray = new char[50000000];
}
catch(std::bad_alloc) {
std::cerr << "Shit, not enough memory" << std::endl;
}
// use bigarray here...
}
int main() {
foo();
}
HTH,
- J.
Thank you for the education everyone! These excellent posts will help me
create the best-possible design.
- dan elliott
On Wed, 17 Nov 2004 19:57:17 -0800, E. Robert Tisdale wrote: Prash wrote:
Stack is there for automatic variables and its size is limited. So, I suggest that you use heap for your data storage.
Actually, automatic storage [the program stack] and free storage [the heap] share the same virtual memory. The typical program stack grows up from the bottom of virtual memory and the heap grows downward to the top of the stack.
The stack size may be limited but can be easily increased using shell commands or compiler options. For example, on a Linux workstation:
> limit stacksize stacksize 10240 kbytes > limit stacksize unlimited > limit stacksize stacksize unlimited
If [you] have performace to consider, then see to it that some time object creation and deletion might take and you should minimize on that; like, instead of allocation 10 small memory chunks, you can create a larger chunk of memory and then divide it by yourself.
Also, I'd suggest you not to use variable sized arrays as they are implemented in libraries using lists or something and access to them is definetly slower than the fixed sized conventional arrays.
No!
C99 style variable size arrays
http://gcc.gnu.org/ml/gcc/2004-05/msg00746.html
are allocated from free storage [the stack] and have performance characteristics similar to "conventional arrays".
Also, maybe you can have a look at the GNU Scientific library as an option for components. www.gnu.org/software/gsl/
You may as well check out the Vector, Signal and Image Processing Library
http://www.vsipl.org/
or, better yet, the High Performance Embedded Computing Software Initiative
http://www.hpec-si.org/
which actually proposes a C++ language binding.
Ah yes! Very helpful. Thank you.
- dan elliott
On Fri, 19 Nov 2004 16:23:33 +0100, Jacek Dziedzic wrote: Daniel L Elliott wrote:
In what way is the "stack" limited in size that the "heap" is not? I sorry if this is something I should have learned as an undergrad.
As far as I understand it, upon the start of your program a fixed amount of memory is denoted as 'stack' and the rest of the memory is denoted as heap. In that way, stack is usually limited to a certain amount -- often only 1MB in order not to waste too much memory that could be used as heap (because you can't shrink the stack if it is not used).
That way you get a limited amount of stack storage. This can bite you in a cruel manner if you don't remember about it. Under Linux, when the stack is overfilled you usually only get "Aborted" and your program dies instantly.
The size of the stack is usually controlled via a compiler switch.
You might try to run these two programs:
// program 1 // This tries to have a 50MB structure on stack // Will most likely crash void foo() { char bigarray[50000000]; bigarray[0]=0; // use bigarray to make sure the // compiler does not optimize it away }
int main() { foo(); }
// program 2 // This tries to allocate a 50MB structure on the heap // Will most likely succeed, if you have 50MB spare memory #include<iostream> void foo() { try{ char* bigarray = new char[50000000]; } catch(std::bad_alloc) { std::cerr << "Shit, not enough memory" << std::endl; } // use bigarray here... }
int main() { foo(); }
HTH, - J. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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;
};
|
by: balu |
last post by:
Hi Gurus
I need a help. Im developing a C++ libaray. I don't
whether user creates object in stack/heap. Is there a
way to find object location?
Balu.N
|
by: Nick McCamy |
last post by:
I have a question related to allocating on the stack.
In this program below, are my following assumptions true?
- variable a is allocated on the heap since it's static
- variable b is...
|
by: gmccallum |
last post by:
General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.
Question:
What if a struct contains a string...
| |
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...
|
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...
|
by: coder_lol |
last post by:
Thanks everybody for helping me with the Syntax confusion! The
implicit conversion stuff really got me :) I have one more
question...
Array<int32ia;
Does the above use the default...
|
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,...
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |