2

ヒープ アドレスの増加についていくつか実験を行っていたところ、興味深いことが起こりました。(OS:CentOS、)

しかし、私は理解していません、なぜこれが起こったのですか?ありがとう!

これは私が最初にしたことです:

double *ptr[1000];
for (int i=0;i<1000;i++){
    ptr[i] = new double[**10000**];
    cout << ptr[i] << endl;
}

出力はインクリメンタルです (最後の数行):

....
....
0x2481be0
0x2495470
0x24a8d00
0x24bc590
0x24cfe20
0x24e36b0
0x24f6f40
0x250a7d0
0x251e060

次に、10000 を 20000 に変更しました。

double *ptr[1000];
for (int i=0;i<1000;i++){
    ptr[i] = new double[**20000**];
    cout << ptr[i] << endl;
}

アドレスは、スタック スペースのアドレス (およびデクリメンタル) のようになりました。

....
....
0x7f69c4d8a010
0x7f69c4d62010
0x7f69c4d3a010
0x7f69c4d12010
0x7f69c4cea010
0x7f69c4cc2010
0x7f69c4c9a010
0x7f69c4c72010
0x7f69c4c4a010
0x7f69c4c22010
0x7f69c4bfa010
0x7f69c4bd2010
0x7f69c4baa010
0x7f69c4b82010
4

5 に答える 5

4

新しい関数は、メモリを割り当てたい任意の方法を選択できるため、ここで良い答えを得ることはできません。私の推測では、ここでのアルゴリズムはプールを小さな割り当てプールと大きな割り当てプールに分割し、大きな割り当てプールは下向きに成長するため、(スペースを無駄にしないように) 中間で会うことができます。

于 2013-04-16T02:41:50.603 に答える
0

You were lucky in the sense that the sizes of 10000 doubles and 20000 doubles happen to lie on the opposite sides of a critical thresholds called MMAP_THRESHOLD.

MMAP_THRESHOLD is 128KB by default. So, 80KB (i.e., 10000 doubles) mem alloc requests are serviced over heap, and whereas 160KB (20000 doubles) mem alloc requests are serviced by anonymous memory mapping (through mmap sys call). (Note that using mem mapping for large mem alloc may incur additional penalties due to its different underlying mem alloc handling mechanism. You may want to tune MMAP_THRESHOLD for optimal performance of your apps.)

In Linux Man for malloc:

Normally, malloc() allocates memory from the heap, and adjusts the size of the heap as required, using sbrk(2). When allocating blocks of memory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation allocates the memory as a private anonymous mapping using mmap(2). MMAP_THRESHOLD is 128 kB by default, but is adjustable using mallopt(3). Allocations performed using mmap(2) are unaffected by the RLIMIT_DATA resource limit (see getrlimit(2)).

于 2015-08-20T19:13:03.627 に答える