lkml.org 
[lkml]   [1999]   [Dec]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: Recursive malloc crashing Linux. (Well almost)
Date
On Tue, 14 Dec 1999, C Hanish Menon wrote:
> A friend of mine actually tried a small program which had a recursive function
> with malloc in it, In which unknowingly he had forgotten to terminate the
> recursion thus going into a infinite recursion.
>
> When he ran this program , it used to seg fault , but at the same time he found
> that he had lost space in his hard disk. This was inspite of removing the
> Core Dumps it generated.
...
> When he asked me, I also wrote a similar program and tried on my m/c.
> Strangly enough I got 2 behaviours.
> a) Sometimes it gives a Bus error
> b) Sometimes everything almost hangs. I mean It starts giving
...
> This seemed strange enough so I thought I check if anyone else has
> noticed this before.
>
> Well isn't the memory allocation logic such (or shouldn't it be ) that if a
> process starts to ask beyond a certain amount of total memory it stops giving
> any more memory to the process or so.
>
> *************** the Culprit code ***************
>
> void recurse(void)
> {
> malloc(4096*1024);
> recurse();
> }
>
> int main()
> {
> recurse();
> return 0;
> }

I've encountered this problem too, but with the additional restriction of
having an environment with 8MB of RAM and no swap. I'm not sure I agree
with the ability to allocate more memory than you have available. So long
as each individual allocation is less than total memory, it seems to
succeed. When you get round to using it... it's not there of course, and
you get a SIGBUS or blocking. Sometimes it does nicely return ENOMEM and
malloc() gracefully returns - but most of the time it's a SIGBUS.

Looking at vm_enough_memory() (mm/mmap.c) it looks like only accessed
pages are counted against the amount of free memory (free + buffer +
cache + swap). A 'fix' would perhaps be to have a count of how many pages
are unpaged but allocated.

As another note: locked mmaps of files also seem to be counted towards
this figure - but locked pages can't be paged (SIGBUS - can't handle
kernel paging request when enough memory appears to be free). Locked mmaps
are also paged in on allocation even if they're not read/write (glibc
allocates 2MB at a time for thread heaps to get 1MB vm alignment - with 8MB
of RAM this means leaving a lot of spare floating about).

--
John Ripley, empeg Ltd.
http://www.empeg.com

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:55    [W:0.154 / U:0.128 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site