Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdbool.h>
- // for memset
- #include <string.h>
- #include <proc/readproc.h>
- #include <sys/resource.h>
- #include <time.h>
- // for malloc_trim
- #include <malloc.h>
- #define STEP 6
- #define CHUNK (STEP * 1024)
- #define PAGE_SIZE sysconf(_SC_PAGESIZE)
- /*
- * Platform pecific function. Return amount of pysical memory on this system;
- */
- int64_t getTotalSystemMemory()
- {
- return (int64_t) sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
- }
- /*
- * Function write to allocated memory block Zeros.
- * Return pointer to allocated block.
- */
- void *allocate_memory (size_t size) {
- void *p = malloc(size);
- if (p != NULL)
- {
- memset(p, 0, size);
- }
- return p;
- }
- /*
- * Function checking if process is swapped.
- * Must run less then each iteration of malloc'ing to check it.
- */
- bool is_swapped()
- {
- getrusage(RUSAGE_SELF, &r);
- if (__rss != r.ru_maxrss)
- {
- __rss = r.ru_maxrss;
- return false;
- }else {
- return true;
- }
- }
- static int64_t __rss;
- struct rusage r;
- int64_t last_chunk;
- int64_t i;
- clock_t start;
- clock_t malloc_time,free_time;
- int main()
- {
- __rss = r.ru_maxrss;
- int64_t phys_mem = getTotalSystemMemory();
- printf("Total installed memory:\t%lld bytes\n", phys_mem);
- struct proc_t p;
- look_up_our_self(&p);
- long unsigned int rss_rlim = p.rss_rlim;
- int64_t total_mem;
- // geting the must available memory limit, to avoid
- // segfault in memset, when using PAE.
- if (rss_rlim > phys_mem)
- {
- total_mem = phys_mem;
- }else {
- total_mem = rss_rlim;
- }
- int64_t amount_chunks = total_mem / CHUNK;
- printf("total_available_mem:\t%lld bytes\n", total_mem);
- printf("Calculated chunks:\t%u pcs\n", amount_chunks);
- // allocating memory for array with pointers of memory
- void **chunks = malloc(sizeof(void *) * amount_chunks );
- printf("Start grabbing\n");
- // timer
- start = clock();
- for (i=0;i<amount_chunks;i++)
- {
- // write pointer of allocated memory chunk to array
- chunks[i] = allocate_memory(CHUNK);
- last_chunk = i;
- // checking, if malloc return NULL pointer, after that we can't allocate more memory. Stoping
- if (chunks[i] == NULL)
- {
- break;
- }
- // Linux specific swap checker
- if ((i % (24*STEP) == 0) && is_swapped())
- {
- break;
- }
- }
- // timer
- malloc_time = clock() - start;
- printf("Time of malloc'ing %u ms\n", (malloc_time/1000)/1);
- printf("-----------------------------------------------\n");
- printf("Allocated bytes:\t%lld\n", last_chunk*CHUNK);
- printf("Used chunks:\t\t%u\n", last_chunk);
- printf("* Check the current state of allocated memory\n");
- printf("Start free'ing\n");
- // timer
- start = clock();
- for (i=0;i<amount_chunks;i++)
- {
- free(chunks[i]);
- }
- // using this system-call to release the "garbage" memory from process to Operating System
- malloc_trim(0);
- // timer
- free_time = clock() - start;
- printf("Time of free'ing %u ms\n", (free_time/1000)/1);
- printf("* Check the current state of allocated memory\n");
- sleep(5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement