ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What in the world could require one gig of memory? Which programming language and is there any way to optimize your code so you don't need to use so much memory?
<antiroach> i wrote a program that recurses x number of times. when x is 25000 it core dumps. i ran a debugger and it said a stack overflow happened. is there any way i can increase stack space? or how else can i avoid running out of stack space?
<Sparr> antiroach: stop using the stack
<StoneCyph> antiroach: yes, you can increase stack space. how to prevent that problem? don't recurse to a depth of 5 digits.
<antiroach> how can i increase the stack space
<Kniht> antiroach: use an iterative solution instead
<antiroach> it has to be recursive
<antiroach> this algorithm finds a best move given a position.
<Kniht> no, it doesn't have to be
<antiroach> its like a game thing. yes it does. its part of the specs.
<Kniht> any recursive algorithm can be made iterative
<StoneCyph> antiroach: you're not listening. this is like saying "i built a car that goes fast. when i drive it into the wall at 500 miles per hour, it crumples. can i make it stronger?" "yes, but in general, stop driving into walls."
<[eloi]> StoneCyph: no, more like, "can i move the wall back a bit?"
Pretty similar to what you are asking... All in all you cant solve that problem and you need to work around it, unless you intend to expand your computers memory...
Actualy I think this is a valid questions, maybe he do have a program that will use that amount of data? I myself been playing with terraforming rutins, and physics engiens for these worlds.. The worlds themself are much containted on harddrive and swapped up and such, but the "current state" data is huge to, and If I would ever continue my work, its not unreasonble to exspect to use 1Gig of memory for these calculations. Recursive calls kills the stack, yes it does, yes thats a wall you should avoid. But if you have 2Gig in your machine, or 512Mb and 4Gig swap... Why shouldnt you be able to do this: struct stateData[1000000000] currentState.. ?
I use Fortran for scientific computing. My PC has 2 giga bytes memory.
Quote:
Originally posted by Palin What in the world could require one gig of memory? Which programming language and is there any way to optimize your code so you don't need to use so much memory?
Sorry, It is my fault. I should have told you I have 2 giga bytes memory fixed in my PC. And if I reduce the size of that array to 800MB, I can run the program with 2 threads at the same time, which cost 1.5GB memory.
And also I can change the stacksize to unlimited, but it still refuse to work.
Quote:
Originally posted by Stack Pretty similar to what you are asking... All in all you cant solve that problem and you need to work around it, unless you intend to expand your computers memory...
I take it your allocation is more complicated than:
char *i = new char[1000000000];
I'm not familiar with fortran, is it possible to allocate memory both on the stack and on the heap? The above C example is stack allocation, this is heap allocation:
char i[100000000];
Couldn't you try to do just the allocation part without the rest of the program logic? I'm sorry I can't do it myself. My bad excuse for a computer has only 192 MB
We don't know if your FORTRAN compiler allocates arrays on the stack on in heap memory. There is a BIG difference. Allocating to heap is the way to go for large arrays.
Are there compiler switches that change how memory allocates for variables? ie., make the array static (heap), not volatile.
PS: some FORTRAN compilers used to have support for virtual arrays - ie., arrays kept mostly on disk and paged into/out of memory as needed. I don't know if your FORTRAN compiler does that, but it's worth investigating.
Originally posted by nowonmai the above C example is C++
Whoops Sorry. I think it's pretty obvious what my language of choice is now...
I took the liberty to test this a little. This code snippet (which is C++) shows what happens on my machine:
Code:
try
{
char *i = new char[1000000000]; // 1GB
}
catch(bad_alloc)
{
cout << "Caught a bad alloc exception" << endl;
}
Without the try block, the program exits with a core dump. Otherwise the exception line is printed.
One interesting thing to note is that I can easily allocate 300 MB 3 times, like this:
Code:
char *i = new char[300000000]; // 300 MB
char *j = new char[300000000];
char *k = new char[300000000];
The memory is never really allocated as physical memory until I start using it. Every memory request is checked to see if it can be satisfied, but the allocator routine doesn't know about other requests that haven't been used.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.