tags:

views:

282

answers:

3

Please tell me C++/Java code which utilize memory more than 70% .

For Example we have 3 Virtual machine and in memory resources we want to test the
memory utilization as per memory resources allocated by user.

+3  A: 
#include malloc.h
#DEFINE MB 512
void main(int argc, char **argv)
{
    int i;
    for (i = 0; i < MB; i++)
    {
        malloc(1024* 1024);
    }
    getchar();
}

Hit enter to release the memory, set the MB constant to how much memory you want your program to take.

My C is a little rusty so if someone comes here and walks all over me, 1000 apologies, my forte is C#.

Spence
You dont' need a loop. Just do a malloc(1024 * 1024 * MB);
Marc
No, that doesn't work on fragmented heaps. The loop won't allocate everything either, but you are unlikely to loose close to 30% in fragmentation when allocating 1MB blocks.
MSalters
+4  A: 

Which memory? On a 64 bit platform, a 64 bit process can use far more than 4GB. You'd be filling swap for hours before you hit those limits.

If you want to test "70% of physical RAM", you might discover that you cannot allocate 70% of the 32 bits address space. A significant amount is already claimed by the OS.

MSalters
A: 

I want to test the Memory utilization but after executing the code i am unable to test the same.

As i am new to this so help me more on this.

Let we have 3 Virtual machine V1,V2,V3

For V1 - Set shared resource as High

For V2 - Set shared resources as Normal

For V3 - Set shared resources as Normal

So it means total is 2 GB then V1 get 1 GB and V2,V3 gets 512 MB each . So i want to test using programming if some one changes the Shared or reservation or Limit then how it works.