我編寫了一個(gè)簡單的程序來研究在Linux(64位Red Hat Enterprise Linux Server 6.4版)上使用大量RAM時(shí)的性能。(請忽略內(nèi)存泄漏。)#include <sys/time.h>#include <time.h>#include <stdio.h>#include <string.h>#include <iostream>#include <vector>using namespace std;double getWallTime(){ struct timeval time; if (gettimeofday(&time, NULL)) { return 0; } return (double)time.tv_sec + (double)time.tv_usec * .000001;}int main(){ int *a; int n = 1000000000; do { time_t mytime = time(NULL); char * time_str = ctime(&mytime); time_str[strlen(time_str)-1] = '\0'; printf("Current Time : %s\n", time_str); double start = getWallTime(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = 1; } double elapsed = getWallTime()-start; cout << elapsed << endl; cout << "Allocated." << endl; } while (1); return 0;}輸出是Current Time : Tue May 8 11:46:55 20183.73667Allocated.Current Time : Tue May 8 11:46:59 201864.5222Allocated.Current Time : Tue May 8 11:48:03 2018110.419頂部輸出如下。我們可以看到盡管有足夠的可用RAM,但是交換增加了。結(jié)果是運(yùn)行時(shí)間從3秒猛增到64秒。我的問題是為什么Linux會(huì)犧牲性能而不是全部使用緩存的RAM?內(nèi)存碎片?但是,將數(shù)據(jù)放在交換上也肯定會(huì)造成碎片。是否有一種解決方法可以使3秒鐘保持一致,直到達(dá)到物理RAM大?。?
添加回答
舉報(bào)
0/150
提交
取消