LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2005, 04:25 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
My stupid code adventure


Hello everyone

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.


I know this is probably a stupid question.
 
Old 01-16-2005, 05:29 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.
 
Old 01-16-2005, 06:06 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 01-16-2005, 11:36 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Linux supplies ulimit
Code:
itsme@dreams:~$ ulimit -a
core file size (blocks)     unlimited
data seg size (kbytes)      unlimited
file size (blocks)          unlimited
max locked memory (kbytes)  unlimited
max memory size (kbytes)    unlimited
open files                  1024
pipe size (512 bytes)       8
stack size (kbytes)         8192
cpu time (seconds)          unlimited
max user processes          6144
virtual memory (kbytes)     unlimited
itsme@dreams:~$
 
Old 01-17-2005, 09:54 AM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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
 
Old 01-17-2005, 10:35 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Could you output the value of count in testbit == NULL condition ?
Code:
if(testbit == NULL) {
    printf("Cannot allocate space\n");
    printf("count = %l\n", count);
}
 
Old 01-17-2005, 10:48 AM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
are you asking me to impliment this and give the output here ?
 
Old 01-18-2005, 02:24 AM   #8
jeorj_euler
LQ Newbie
 
Registered: Jan 2005
Location: MidWest United States
Distribution: Mandrake, Knoppix
Posts: 9

Rep: Reputation: 0
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?
 
Old 01-18-2005, 02:35 AM   #9
jeorj_euler
LQ Newbie
 
Registered: Jan 2005
Location: MidWest United States
Distribution: Mandrake, Knoppix
Posts: 9

Rep: Reputation: 0
This can happen with linked structures as you well know.

Code:
struct node
{
        node_t data;
        struct node *next;
};
void form_vector (struct node **const x, int const n)
{
        if (n > 0x0)
        {
                int i;
                // cursor pointer
                struct node1 *r;
                // allocation node zero
                *x = (struct node*) malloc(sizeof(struct node));
                r = *x;
                                                                                                                                                       
        // allocation node subsequent
        for (i = 0x1; i < n; i++)
        {
                r->next = (struct node*) malloc(sizeof(struct node));
                r = r->next;
        }
        }
}
Should n be a very large number or should node_t be a massive data type, more memory will be consumed.
 
Old 01-18-2005, 12:26 PM   #10
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Text adventure command problem MylesCLin Programming 6 02-11-2005 04:22 AM
Looking for a new adventure corbintechboy *BSD 12 10-24-2004 05:19 AM
Adventure Games on Linux? henrikanttonen Linux - Games 2 08-27-2004 08:18 PM
want to experience the adventure.Help me cybercop12us Linux - Networking 1 10-23-2001 09:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration