我写了一个简单的程序,应该运行两个线程,对小数组(约4096字节)进行排序并写入输出文件。输入数据包含在一个大文件中(约4GB)。计算机内存为128MB。我发现仅运行空主函数就会使用14MB内存。如果运行带有空函数的std :: thread,则每个线程开始使用约8MB。但是,如果我只进行一次动态内存分配,程序启动时每个线程将使用约64MB。我不明白可能会花费这么多内存。如何控制此大小?如何分配动态内存以最小化某些系统默认分配?
- System: Ubuntu 14.04.3
- Compiler: gcc 4.8.4
Compiler option:'-std=c++11 -O3 -pthread'
This is a code example
void dummy(void) { std::vector<unsigned int> g(1); int i = 0; while( i<500000000) { ++i; } } int main(void) { std::thread t1(&dummy); std::thread t2(&dummy); std::thread t3(&dummy); t1.join(); t2.join(); t3.join(); return 0; }
ulimit -s
,你会得到什么? - Rama