Вопрос

I want to test how my system performs when the CPU utilization is near to 99% and also the memory utilization is also near to 99%.

To increase the memory and cpu utilization to that extent can I write some code in C++ or anything?

My machine is a Ubuntu Server 12.04 with no GUI.

Это было полезно?

Решение

Here is what you can do:

#include <array>

const unsigned int BIG_SIZE = 50 * 1024 * 1024;

int main()
{
    std::array<char, BIG_SIZE> *m = new std::array<char, BIG_SIZE>;



    std::fill(m->begin(),m->end(),0);

    while(1)
        ;
}

The while loop with use 100% of a single-core CPU.

The array object will eat up the memory. You have to touch it, otherwise the OS will not really assign you that memory.

Другие советы

for CPU kill i usually do something like this:

volatile int thread_run=0;
volatile int thread_stop=0;
DWORD __stdcall CPU_Kill_thread(LPVOID *p)
 {
 thread_run++;
 for(;!thread_stop;)
  {
  }
 thread_run--;
 }

void stop() // stop the threads before app exit
 {
 for (thread_stop=1;thread_run;) Sleep(1);
 }
  • launch 1 thread for each CPU and set their affinity to appropriate CPU !!!

for memory Kill:

  • inside main program (or thread)

    1.size=1<<20; = 1MB or whatever

    2.allocate dat=new BYTE[size]

    3.if (dat==NULL) allocate siz/2 size and stop

    4.delete dat; siz<<=1; goto 2

  • now just do some memory access on whole dat[]

  • do not need to access all bytes
  • you can do it for every 13th byte or whatever (should be odd number just to be sure)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top