Swappiness 用於積極交換

Swappiness 用於積極交換

我想為 Ubuntu 14.04 配置激進的交換行為。我使用的是 Linux 核心 3.14。我使用以下命令修改了 swappiness。

echo 100 > /proc/sys/vm/swappiness

然而,作業系統似乎不會交換頁面,直到主機耗盡可用記憶體。如何讓作業系統主動將未造訪的頁面交換到交換設備?

答案1

將公羊用於其他用途。分配記憶體或嘗試分配快取的磁碟 IO 的進程將推出空閒頁面進行交換以騰出空間。 swappiness 變數只會讓系統更傾向於將應用程式推出來使用 ram 作為檔案系統快取。

答案2

為什麼要這麼做?

無論如何,如果這就是你想要的,那就是你會得到的:

#include <stdio.h>
#include <sys/sysinfo.h>

fGetMyRAM()
{
    FILE* fStream = popen( "head -n1 /proc/meminfo", "r" );
    std::ostringstream output;
    int iBuffer = 128;

    while( !feof( fStream ) && !ferror( fStream ))
    {
        char cBuffer[iBuffer];
        int iBytesRead = fread( cBuffer, 1, iBuffer, fStream );
        output.write( cBuffer, iBytesRead );
    }
    std::string sResult = output.str();

    std::string sLabel, sRAM;
    std::istringstream iss(sResult);
    iss >> sLabel;
    iss >> sRAM;

    return std::stoi( sRAM );
}

int main()
{
  int iMyRAM = fGetMyRAM();
  int *pMemoryHog;

  while (true)
  {
    //as malloc alocates largest contiguous memory, keep slicing down.
    iMyRam = iMyRam/2;

    printf("%d\n", iMyRam);

    pMemoryHog = (int *) malloc ((iMyRam)*sizeof(void *));

    if (pMemoryHog == 0)
    {
      printf("Yeah! NO MORE MEMORY!\n");
      return 1;
    } // end if out of memory
    strcpy(pMemoryHog, "42");
  } // end while true
  free(pMemoryHog);
  return 0;
} // end main

相關內容