C - wanted to know the maximum amount of memory allocated in the program

I'm new to C

I wanted to know the maximum memory allowed by the application. Therefore, I wrote a small program, for example the following.

I have a machine with 16 GB and 2 GB shared memory, and 14 GB is free. I expected this program to stop at about 14 GB, but it works forever.

Do you want me to do something wrong here?

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

int main(){
    long total = 0;
    void* v = malloc(1024768);

    while(1) {
        total += 1024768;
        printf ( "Total Memory allocated : %5.1f GB\n", (float)total/(1024*1024768) );
        v = realloc(v, total);
        if (v == NULL) break;
    }
} 

Edit: Run this program on CentOS 5.4 64 bit.

+3
source share
3 answers

On most modern operating systems, memory is allocated for each page used, and not for each page that is "reserved." Your code does not use any pages, so no memory is allocated.

memset; , .

, . !

+5

, ?

, , , , 16 , 64-. , , 1/ 2/ .

32- Windows 4 . 64- Windows 16 ( , , ).

http://support.microsoft.com/kb/294418

YMMV .

: ruslik , 2 3 ( , ) 32- Windows. KB, , , , 3 4 , 1 , .

+4

/, , .

- eritiong, . :

  • : , .
  • , , VMM.
  • The algorithm can change the internal state of the MM, so the available memory before and after the call may differ.
  • Since the OS runs several processes in parallel, the available memory can be changed spontaneously due to other activity of the process.
+1
source

Source: https://habr.com/ru/post/1772311/


All Articles