Advertisement
elvman

Untitled

Apr 6th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <inttypes.h>
  5. #include <stdint.h>
  6.  
  7. #define MEGABYTE 1024*1024
  8.  
  9. static uint64_t getTotalSystemMemory()
  10. {
  11.     long pages = sysconf(_SC_AVPHYS_PAGES);
  12.     long page_size = sysconf(_SC_PAGE_SIZE);
  13.     return pages * page_size / MEGABYTE;
  14. }
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     void *myblock = NULL;
  19.     int count = 0;
  20.  
  21.     while (1)
  22.     {
  23.         myblock = (void *) malloc(MEGABYTE);
  24.         if (!myblock) break;
  25.         printf("Currently allocating %d MB\n", ++count);
  26.         printf("Free memory: %" PRId64 " MB\n", getTotalSystemMemory());
  27.         nanosleep(1000 * 1000 * 2);
  28.     }
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement