0

The host OS is a Windows 10, with 16BG RAM. I have Oracle VirtualBox with Ubuntu 18.04 as the guest OS (2GB RAM).

I ran the following program on the guest OS. I could see top listing up to 40GB. But, I did not see the RAM usage of the VirtualBox going beyond 230MB.

How can I see the exact memory counters on the guest and the host?

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

static size_t total = 0, n = 0;
static char* ptrs[20000] = {0};
static size_t ncur  = 0;

static void alloc_more(int more)
{
    char* tmp = (char*)malloc(more);
    tmp[more/2] = 32;
    tmp[more-1] = 41;
    tmp[0] = 53;
    total += more;
    printf("\n allocated total is %lu GB for %p", total/(1024*1024*1024),tmp);
    ptrs[++n] = tmp;
    ncur = (n*3)/4;
    if(ptrs[ncur])
    {   memset((void*)ptrs[ncur], 33, more);
    }
}

int main(int argc, char* argv[])
{
    while(1)
    {
        alloc_more(atoi(argv[1]));
    }
    return 0;
}

1 Answer 1

1

When you allocate the memory, it merely gets reserved. You need to put something in it before it actually gets used. If you put something in it, then you should observe the VM RAM usage increase. You may want to slow things down with some sleeps in your program to give yourself time to observe things as it proceeds.

There was an interesting question about using all the memory available just the other day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.