[comp.lang.c] Finding/changing max memory size available

ric@ace.sri.com (Richard Steinberger) (05/28/91)

The following short program finds the maximum amount of memory that can be
allocated through use of malloc().  What I'd like to know is, "Where/How
is this limit set?  Is this a a function of 1 or more kernel parameters,
or other system configuration variables?"  Can the upper allocatable memory
limit be changed (without adding physical memory, of course)?  Thanks for
any replies.

regards,

	ric steinberger
	ric@ace.sri.com

/****************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#define MALLOC_SIZE 1000000L        /* Number of bytes to allocate / call */

main()
{
  char *cptr;
  long i;

  while ((cptr = malloc(MALLOC_SIZE)) != NULL)
    i++;

  printf("Total memory allocated = %u bytes.\n", i*MALLOC_SIZE);
  exit(0);
}

/****************************************************************************/

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (05/30/91)

In article <24817@unix.SRI.COM>, ric@ace.sri.com (Richard Steinberger) writes:

> The following short program finds the maximum amount of memory that
> can be allocated through use of malloc().

[program deleted - it just repeats malloc(1000000L) until failure]

Not quite.  Aside from the bug (malloc takes an int or an unsigned int,
not a long, as an argument), it tells you the number of 1000000-byte
chunks you can allocate.  This may bear less relation than you hope to
the maximum amount of memory allocatable.

> What I'd like to know is, "Where/How is this limit set?  Is this a a
> function of 1 or more kernel parameters, or other system
> configuration variables?"  Can the upper allocatable memory limit be
> changed (without adding physical memory, of course)?

This limit is a complex interaction of several things.

- There's generally a limit on the maximum size of a process' virtual
  address space.  (Set at kernel build time on those few systems I am
  sufficiently familiar with to tell about.)

- There's a limit on the total size of the virtual address spaces of
  all processes.  (Typically this is the amount of swap space
  available, with shared text segments and mmap()ed files (if
  applicable) not counted.)

- There may be a purely software-imposed limit on the maximum size of
  your data segment, depending on your system.  (Check to see if you
  have the setrlimit call, and look for RLIMIT_DATA.)

- There may be an implicit limit built into malloc.

- Probably something else I've forgotten.

Usually, in my experience, the second one is the one you actually hit,
though in some circumstances I've hit the first one instead.

As for how you can change it...that depends on which one you want to
change.  For the first one you will have to consult your OS
documentation; if it is silent, it is probably (at best) a compile-time
parameter of your kernel.  For the second, you'll have to either add
swap space or kill off other processes.  For the third, you should
raise the resource limit with setrlimit.  For the fourth, you'd have to
recompile malloc.  For the fifth I can't help :-)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

steve@taumet.com (Stephen Clamage) (05/31/91)

mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes:

>... malloc takes an int or an unsigned int, not a long, as an argument ...

In ANSI C, malloc takes a parameter of type "size_t" (defined by the
implementation) which must be an unsigned integral type; it is typically
"unsigned int" or "unsigned long".
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

sarima@tdatirv.UUCP (Stanley Friesen) (06/01/91)

In article <1991May30.103731.28593@thunder.mcrcim.mcgill.edu> mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes:
>In article <24817@unix.SRI.COM>, ric@ace.sri.com (Richard Steinberger) writes:
>> What I'd like to know is, "Where/How is this limit set?

>This limit is a complex interaction of several things.
<<excellent list deleted>>
>
>- Probably something else I've forgotten.

mmap() (or shmat()) may map a file or shared memory segment only a few K
above the top user address, efectively placing a roadblock in the way
of sbrk(), and thus limiting malloc().

I have seen this happen - malloc() returning NULL in a large virtual
address space because the next block of addresses was used.
-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)