我想用激进的交换行为配置Ubuntu14.04。我使用的是Linux内核3.14。我使用以下命令修改了swappiness。
echo 100 > /proc/sys/vm/swappiness然而,在主机耗尽空闲内存之前,操作系统似乎不会交换页面。如何使操作系统主动地将未访问的页面交换到交换设备?
发布于 2015-01-04 00:21:14
用公羊做别的事。分配内存或磁盘IO试图分配缓存的进程会将空闲页推送到交换区以腾出空间。swappiness变量只会使系统更倾向于将应用程序推送出去,而倾向于将ram用于文件系统缓存。
发布于 2015-01-04 00:10:18
你为什么要这么做?
不管怎么说,如果这是你想要的,那就是你会得到的:
#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 mainhttps://askubuntu.com/questions/568273
复制相似问题