How to limit the amount of memory accessible to my C code? -


To test just, I ran this code

  #include  Unistd.h & gt; #to & lt include, stdio.h & gt; #to & lt include, stdlib.h & gt; Int main () {int ** ar = (int **) malloc (100000000 * size (int *)); Int i; For (i = 0; i & lt; 10000000; i ++) {r [i] = (int *) molk (1000 * 4); R [i] [123] = 456; Sleep (3000000); Usleep (3000000); Usleep (3000000); Usleep (3000000); Usleep (3000000); Usleep (3000000); Return 0; }  

The Memory Usage Graph went like this (below the pink graph track memory).

alt text

Although this program is not out of memory However, malloc will fail with any more memory requirement, and then ar [i] [123] = 456; A division fault due to the line.

I want to put a limit on memory allocation through my program, but I do not even want to stabilize my schedule.

For example, What is a way to tell your program to use almost half of the available memory on the system (the system where binary was run), but now No more

So, if my program is run on a machine with 8 GB of memory, then it can use a maximum of 4 GB, but if the program runs 256 MB On one machine, only 128 MB should be available for the program.

In addition to this, instead of controlling AMO, I want to do this from within my program unt in available memory using some external utility.

It is generally considered abusive for a program, on the amount of physical memory on a machine and some of its Use the part. What if 10 programs believe that they are entitled to the use of half-machine memory?

A better approach to dealing with the huge versions of data has to work with it on the disk, and the operating system's filesystem cache does optimal utilization of the physical memory of the machine in a way that is useful for many processes. Is reasonable.


Comments