
공격적인 스왑 동작으로 Ubuntu 14.04를 구성하고 싶습니다. 저는 리눅스 커널 3.14를 사용하고 있습니다. 다음 명령을 사용하여 swappiness를 수정했습니다.
echo 100 > /proc/sys/vm/swappiness
그러나 OS는 호스트에 사용 가능한 메모리가 부족할 때까지 페이지를 교체하지 않는 것 같습니다. OS가 액세스되지 않은 페이지를 스왑 장치로 사전에 교체하도록 하려면 어떻게 해야 합니까?
답변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