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.
Ive recently been playing with some malloc code in windows xp
"Actually trying to see if i can break the memory manager"
i wanna know what you guys think of my memory allocator forever
Code:
/* This program will allocate memeory untill there is no more memory left */
/* on the system ********************************************************/
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
unsigned long count = 5;
while ( count != 0)
{
malloc(count);
count++;
}
return 0;
}
What perplexes me while this is purpously bad code :P is when i compile and run the code it allocates memory more and more then windows just seams to set it to 10 mb and stops allocating any more.
Is this cause there is a limit on malloc or because the system memory manager is protecting the system from this memory leek ??
Also would linux detect this buggy program and protect the system from utliziing all of the availible memory.
Perhaps a C programmer can elaborate (and correct me if I'm wrong), but when you use malloc(), you are requesting some memory from the memory manager. If the memory is not available, malloc fails. My guess is that you're hitting the limit of available memory, and malloc is failing. A smart OS isn't going to just let you allocate memory until you completely run out.
You could put in a test, print a message and exit whenever malloc fails, if you want to test this theory.
I know in openbsd there's an adjustable per-process limit on heap size. You can even set it according to what group or user the process is. For root it's unlimited by default, iirc.
On my defaultish debian install I seem to be able to go more than a gigabyte into swap with one non-root process; i'd guess it's unlimited.
Yea thats what is happening it just failes to allocate the memory on the computer.
here is the revised code
Code:
/* This program will allocate memeory untill there is no more memory left */
/* on the system ********************************************************/
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
unsigned long count = 5;
char *testbit;
while ( count != 0)
{
testbit = malloc(count);
count++;
if (testbit == NULL)
{
printf("Cannot allocate space\n");
}
}
return 0;
}
i havent tried this program on linux but on windows xp it goes till it allocates 14 mb of memory then i get the Cannot allocate space message
Interesting. Such rapid allocation on Linux results in consumption of free swap space. I am not sure how swap works precisely, but from what I can tell, the kernel will move pages already in physical memory to swap to make room for a process such as this. The more rapidly a process reads and writes to memory, the more precedence it has in using physical memory. When such a process finally exits or is killed, a lot of physical memory is suddenly freed.
How much Virtual Memory do you have XP configured to use? Could you figure out how to run the process as SYSTEM?
I had widnows xp manageing the swap space on the parttion from what i can tell on my work computer here the system is using about 200 mb of swap on the harddrive. When i run this program i get a message that says " Windows is running out of virutal memory and increased the size of the page file " So windows does the same thing it moves alot of running processes into the swap space and when the program is finially killed or closed alot of physical memory is then freed. I dont know how this could be usefull in a program but maybe it can be used to temporarly force the system to free physical memrory.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.