Click here to Skip to main content
15,991,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The test application creates a new thread each time enter key is pressed.
From the thread I allocate '
int *num = new int[100];
'

After each enter key press, I check Virtual memory using top command in Ubuntu.
It is observed that for each allocation from a new thread, the Virtual mem increases by ~72mb. For multiple allocation from same thread, the virtual memory is not changed.

Here is the code.
<pre lang="C++">
<pre>// Function that each thread will execute
void threadFunction() {

    int *num = new int[100];
    // Sleep for one minute'
    this_thread::sleep_for(chrono::minutes(1));
    cout << "Thread finished sleeping for 1 minute!" << endl;
}


What I have tried:

- Tried using pthread instead of std::thread no use.
- If i allocate in stack instead of new, then there is no issue. But i need to allocate in heap.
- In windows the issue is not observed for the same code
- The pmap output shows below
00007f7b2c021000  65404K -----   [ anon ]
00007f7b34000000    132K rw---   [ anon ]
00007f7b34021000  65404K -----   [ anon ]



- valgrind output

    MB
218.4^                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
     |                                                                       #
   0 +----------------------------------------------------------------------->Mi
     0                                                                   2.202
Number of snapshots: 54
 Detailed snapshots: [9, 13, 23, 33, 42, 47, 52 (peak)]
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  0              0            4,096            4,096             0            0
  1              0           12,288           12,288             0            0
  2              0           16,384           16,384             0            0
  3              0           24,576           24,576             0            0
  4              0           32,768           32,768             0            0
  5              0          204,800          204,800             0            0
  6              0          249,856          249,856             0            0
  7              0          266,240          266,240             0            0
  8              0          270,336          270,336             0            0
  9              0          278,528          278,528             0            0
100.00% (278,528B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->100.00% (278,528B) 0x0: ???
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 10              0          286,720          286,720             0            0
 11              0          290,816          290,816             0            0
 12              0          294,912          294,912             0            0
 13              0          294,912          294,912             0            0
100.00% (294,912B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->98.61% (290,816B) 0x0: ???
|
->01.39% (4,096B) 0x4020290: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 14              0          290,816          290,816             0            0
 15              0          290,816          290,816             0            0
 16         32,304          299,008          299,008             0            0
 17         33,124          319,488          319,488             0            0
 18         33,193          323,584          323,584             0            0
 19         33,235          327,680          327,680             0            0
 20         33,274          335,872          335,872             0            0
 21         36,599          356,352          356,352             0            0
 22         39,268        2,633,728        2,633,728             0            0
 23         39,358        3,751,936        3,751,936             0            0
100.00% (3,751,936B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->92.25% (3,461,120B) 0x4026CB7: __mmap64 (mmap64.c:58)
| ->92.25% (3,461,120B) 0x4026CB7: mmap (mmap64.c:46)
|   ->61.24% (2,297,856B) 0x4008F1B: _dl_map_segment (dl-map-segments.h:29)
|   | ->61.24% (2,297,856B) 0x4008F1B: _dl_map_segments (dl-map-segments.h:102)
|   |   ->61.24% (2,297,856B) 0x4008F1B: _dl_map_object_from_fd (dl-load.c:1258)
|   |     ->61.24% (2,297,856B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |       ->60.70% (2,277,376B) 0x4003494: openaux (dl-deps.c:64)
|   |       | ->60.70% (2,277,376B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |       |   ->60.70% (2,277,376B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |       |     ->60.70% (2,277,376B) 0x402339F: dl_main (rtld.c:1970)
|   |       |       ->60.70% (2,277,376B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |       |         ->60.70% (2,277,376B) 0x4021597: _dl_start_final (rtld.c:507)
|   |       |           ->60.70% (2,277,376B) 0x4021597: _dl_start (rtld.c:596)
|   |       |             ->60.70% (2,277,376B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |       |
|   |       ->00.55% (20,480B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->30.24% (1,134,592B) 0x4009098: _dl_map_segments (dl-map-segments.h:140)
|   | ->30.24% (1,134,592B) 0x4009098: _dl_map_object_from_fd (dl-load.c:1258)
|   |   ->30.24% (1,134,592B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |     ->29.80% (1,118,208B) 0x4003494: openaux (dl-deps.c:64)
|   |     | ->29.80% (1,118,208B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |     |   ->29.80% (1,118,208B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |     |     ->29.80% (1,118,208B) 0x402339F: dl_main (rtld.c:1970)
|   |     |       ->29.80% (1,118,208B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |     |         ->29.80% (1,118,208B) 0x4021597: _dl_start_final (rtld.c:507)
|   |     |           ->29.80% (1,118,208B) 0x4021597: _dl_start (rtld.c:596)
|   |     |             ->29.80% (1,118,208B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |     |
|   |     ->00.44% (16,384B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->00.76% (28,672B) in 1+ places, all below ms_print's threshold (01.00%)
|
->07.75% (290,816B) 0x0: ???
|
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 24         39,400        4,206,592        4,206,592             0            0
 25         39,439        4,263,936        4,263,936             0            0
 26         39,890        4,276,224        4,276,224             0            0
 27         44,400        4,407,296        4,407,296             0            0
 28         44,469        4,501,504        4,501,504             0            0
 29         44,511        4,517,888        4,517,888             0            0
 30         44,550        4,526,080        4,526,080             0            0
 31         49,305        6,791,168        6,791,168             0            0
 32         49,376        8,450,048        8,450,048             0            0
 33         49,418        8,810,496        8,810,496             0            0
100.00% (8,810,496B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->96.70% (8,519,680B) 0x4026CB7: __mmap64 (mmap64.c:58)
| ->96.70% (8,519,680B) 0x4026CB7: mmap (mmap64.c:46)
|   ->53.28% (4,694,016B) 0x4008F1B: _dl_map_segment (dl-map-segments.h:29)
|   | ->53.28% (4,694,016B) 0x4008F1B: _dl_map_segments (dl-map-segments.h:102)
|   |   ->53.28% (4,694,016B) 0x4008F1B: _dl_map_object_from_fd (dl-load.c:1258)
|   |     ->53.28% (4,694,016B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |       ->53.05% (4,673,536B) 0x4003494: openaux (dl-deps.c:64)
|   |       | ->53.05% (4,673,536B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |       |   ->53.05% (4,673,536B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |       |     ->53.05% (4,673,536B) 0x402339F: dl_main (rtld.c:1970)
|   |       |       ->53.05% (4,673,536B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |       |         ->53.05% (4,673,536B) 0x4021597: _dl_start_final (rtld.c:507)
|   |       |           ->53.05% (4,673,536B) 0x4021597: _dl_start (rtld.c:596)
|   |       |             ->53.05% (4,673,536B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |       |
|   |       ->00.23% (20,480B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->42.96% (3,784,704B) 0x4009098: _dl_map_segments (dl-map-segments.h:140)
|   | ->42.96% (3,784,704B) 0x4009098: _dl_map_object_from_fd (dl-load.c:1258)
|   |   ->42.96% (3,784,704B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |     ->42.77% (3,768,320B) 0x4003494: openaux (dl-deps.c:64)
|   |     | ->42.77% (3,768,320B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |     |   ->42.77% (3,768,320B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |     |     ->42.77% (3,768,320B) 0x402339F: dl_main (rtld.c:1970)
|   |     |       ->42.77% (3,768,320B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |     |         ->42.77% (3,768,320B) 0x4021597: _dl_start_final (rtld.c:507)
|   |     |           ->42.77% (3,768,320B) 0x4021597: _dl_start (rtld.c:596)
|   |     |             ->42.77% (3,768,320B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |     |
|   |     ->00.19% (16,384B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->00.46% (40,960B) in 1+ places, all below ms_print's threshold (01.00%)
|
->03.30% (290,816B) 0x0: ???
|
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 34         49,457        8,835,072        8,835,072             0            0
 35         49,733        8,888,320        8,888,320             0            0
 36         54,859        9,834,496        9,834,496             0            0
 37         54,928       10,342,400       10,342,400             0            0
 38         54,970       10,715,136       10,715,136             0            0
 39         55,009       10,723,328       10,723,328             0            0
 40         74,214       10,731,520       10,731,520             0            0
 41         83,525       10,743,808       10,743,808             0            0
 42      2,166,500       10,743,808       10,743,808             0            0
100.00% (10,743,808B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->97.29% (10,452,992B) 0x4026CB7: __mmap64 (mmap64.c:58)
| ->97.29% (10,452,992B) 0x4026CB7: mmap (mmap64.c:46)
|   ->52.50% (5,640,192B) 0x4008F1B: _dl_map_segment (dl-map-segments.h:29)
|   | ->52.50% (5,640,192B) 0x4008F1B: _dl_map_segments (dl-map-segments.h:102)
|   |   ->52.50% (5,640,192B) 0x4008F1B: _dl_map_object_from_fd (dl-load.c:1258)
|   |     ->52.50% (5,640,192B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |       ->52.31% (5,619,712B) 0x4003494: openaux (dl-deps.c:64)
|   |       | ->52.31% (5,619,712B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |       |   ->52.31% (5,619,712B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |       |     ->52.31% (5,619,712B) 0x402339F: dl_main (rtld.c:1970)
|   |       |       ->52.31% (5,619,712B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |       |         ->52.31% (5,619,712B) 0x4021597: _dl_start_final (rtld.c:507)
|   |       |           ->52.31% (5,619,712B) 0x4021597: _dl_start (rtld.c:596)
|   |       |             ->52.31% (5,619,712B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |       |
|   |       ->00.19% (20,480B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->43.73% (4,698,112B) 0x4009098: _dl_map_segments (dl-map-segments.h:140)
|   | ->43.73% (4,698,112B) 0x4009098: _dl_map_object_from_fd (dl-load.c:1258)
|   |   ->43.73% (4,698,112B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |     ->43.58% (4,681,728B) 0x4003494: openaux (dl-deps.c:64)
|   |     | ->43.58% (4,681,728B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |     |   ->43.58% (4,681,728B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |     |     ->43.58% (4,681,728B) 0x402339F: dl_main (rtld.c:1970)
|   |     |       ->43.58% (4,681,728B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |     |         ->43.58% (4,681,728B) 0x4021597: _dl_start_final (rtld.c:507)
|   |     |           ->43.58% (4,681,728B) 0x4021597: _dl_start (rtld.c:596)
|   |     |             ->43.58% (4,681,728B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |     |
|   |     ->00.15% (16,384B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->01.07% (114,688B) in 3 places, all below massif's threshold (1.00%)
|
->02.71% (290,816B) 0x0: ???
|
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 43      2,166,500       10,739,712       10,739,712             0            0
 44      2,172,421       10,858,496       10,858,496             0            0
 45      2,287,559       19,251,200       19,251,200             0            0
 46      2,295,027      153,468,928      153,468,928             0            0
 47      2,295,048      153,468,928      153,468,928             0            0
100.00% (153,468,928B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->92.92% (142,610,432B) 0x4BB2A27: __mmap64 (mmap64.c:58)
| ->92.92% (142,610,432B) 0x4BB2A27: mmap (mmap64.c:46)
|   ->87.46% (134,217,728B) 0x4B35C81: alloc_new_heap (arena.c:518)
|   | ->87.46% (134,217,728B) 0x4B3616B: new_heap (arena.c:576)
|   |   ->87.46% (134,217,728B) 0x4B3616B: _int_new_arena (arena.c:744)
|   |     ->87.46% (134,217,728B) 0x4B3616B: arena_get2.part.0 (arena.c:963)
|   |       ->87.46% (134,217,728B) 0x4B38A40: arena_get2 (arena.c:931)
|   |         ->87.46% (134,217,728B) 0x4B38A40: tcache_init.part.0 (malloc.c:3244)
|   |           ->87.46% (134,217,728B) 0x4B391DD: tcache_init (malloc.c:3241)
|   |             ->87.46% (134,217,728B) 0x4B391DD: malloc (malloc.c:3306)
|   |               ->87.46% (134,217,728B) 0x48F698B: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
|   |                 ->87.46% (134,217,728B) 0x10938D: threadFunction() (main.cpp:11)
|   |                   ->87.46% (134,217,728B) 0x10A204: void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) (invoke.h:61)
|   |                     ->87.46% (134,217,728B) 0x10A1B0: std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) (invoke.h:96)
|   |                       ->87.46% (134,217,728B) 0x10A151: void std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) (std_thread.h:259)
|   |                         ->87.46% (134,217,728B) 0x10A121: std::thread::_Invoker<std::tuple<void (*)()> >::operator()() (std_thread.h:266)
|   |                           ->87.46% (134,217,728B) 0x10A101: std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() (std_thread.h:211)
|   |                             ->87.46% (134,217,728B) 0x4924252: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
|   |                               ->87.46% (134,217,728B) 0x4B28AC2: start_thread (pthread_create.c:442)
|   |                                 ->87.46% (134,217,728B) 0x4BB9A03: clone (clone.S:100)
|   |
|   ->05.47% (8,392,704B) 0x4B29706: allocate_stack (allocatestack.c:370)
|     ->05.47% (8,392,704B) 0x4B29706: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
|       ->05.47% (8,392,704B) 0x4924328: std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
|         ->05.47% (8,392,704B) 0x109A15: std::thread::thread<void (&)(), , void>(void (&)()) (std_thread.h:143)
|           ->05.47% (8,392,704B) 0x109498: main (main.cpp:28)
|
->06.80% (10,432,512B) 0x4026CB7: __mmap64 (mmap64.c:58)
| ->06.80% (10,432,512B) 0x4026CB7: mmap (mmap64.c:46)
|   ->03.68% (5,640,192B) 0x4008F1B: _dl_map_segment (dl-map-segments.h:29)
|   | ->03.68% (5,640,192B) 0x4008F1B: _dl_map_segments (dl-map-segments.h:102)
|   |   ->03.68% (5,640,192B) 0x4008F1B: _dl_map_object_from_fd (dl-load.c:1258)
|   |     ->03.68% (5,640,192B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |       ->03.66% (5,619,712B) 0x4003494: openaux (dl-deps.c:64)
|   |       | ->03.66% (5,619,712B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |       |   ->03.66% (5,619,712B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |       |     ->03.66% (5,619,712B) 0x402339F: dl_main (rtld.c:1970)
|   |       |       ->03.66% (5,619,712B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |       |         ->03.66% (5,619,712B) 0x4021597: _dl_start_final (rtld.c:507)
|   |       |           ->03.66% (5,619,712B) 0x4021597: _dl_start (rtld.c:596)
|   |       |             ->03.66% (5,619,712B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |       |
|   |       ->00.01% (20,480B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->03.06% (4,698,112B) 0x4009098: _dl_map_segments (dl-map-segments.h:140)
|   | ->03.06% (4,698,112B) 0x4009098: _dl_map_object_from_fd (dl-load.c:1258)
|   |   ->03.06% (4,698,112B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |     ->03.05% (4,681,728B) 0x4003494: openaux (dl-deps.c:64)
|   |     | ->03.05% (4,681,728B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |     |   ->03.05% (4,681,728B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |     |     ->03.05% (4,681,728B) 0x402339F: dl_main (rtld.c:1970)
|   |     |       ->03.05% (4,681,728B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |     |         ->03.05% (4,681,728B) 0x4021597: _dl_start_final (rtld.c:507)
|   |     |           ->03.05% (4,681,728B) 0x4021597: _dl_start (rtld.c:596)
|   |     |             ->03.05% (4,681,728B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |     |
|   |     ->00.01% (16,384B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->00.06% (94,208B) in 1+ places, all below ms_print's threshold (01.00%)
|
->00.28% (425,984B) in 1+ places, all below ms_print's threshold (01.00%)
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 48      2,295,048      153,464,832      153,464,832             0            0
 49      2,295,060      109,072,384      109,072,384             0            0
 50      2,306,901       94,752,768       94,752,768             0            0
 51      2,308,886      228,970,496      228,970,496             0            0
 52      2,308,909      228,970,496      228,970,496             0            0
100.00% (228,970,496B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->95.26% (218,112,000B) 0x4BB2A27: __mmap64 (mmap64.c:58)
| ->95.26% (218,112,000B) 0x4BB2A27: mmap (mmap64.c:46)
|   ->87.93% (201,326,592B) 0x4B35C81: alloc_new_heap (arena.c:518)
|   | ->87.93% (201,326,592B) 0x4B3616B: new_heap (arena.c:576)
|   |   ->87.93% (201,326,592B) 0x4B3616B: _int_new_arena (arena.c:744)
|   |     ->87.93% (201,326,592B) 0x4B3616B: arena_get2.part.0 (arena.c:963)
|   |       ->87.93% (201,326,592B) 0x4B38A40: arena_get2 (arena.c:931)
|   |         ->87.93% (201,326,592B) 0x4B38A40: tcache_init.part.0 (malloc.c:3244)
|   |           ->87.93% (201,326,592B) 0x4B391DD: tcache_init (malloc.c:3241)
|   |             ->87.93% (201,326,592B) 0x4B391DD: malloc (malloc.c:3306)
|   |               ->87.93% (201,326,592B) 0x48F698B: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
|   |                 ->87.93% (201,326,592B) 0x10938D: threadFunction() (main.cpp:11)
|   |                   ->87.93% (201,326,592B) 0x10A204: void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) (invoke.h:61)
|   |                     ->87.93% (201,326,592B) 0x10A1B0: std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) (invoke.h:96)
|   |                       ->87.93% (201,326,592B) 0x10A151: void std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) (std_thread.h:259)
|   |                         ->87.93% (201,326,592B) 0x10A121: std::thread::_Invoker<std::tuple<void (*)()> >::operator()() (std_thread.h:266)
|   |                           ->87.93% (201,326,592B) 0x10A101: std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() (std_thread.h:211)
|   |                             ->87.93% (201,326,592B) 0x4924252: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
|   |                               ->87.93% (201,326,592B) 0x4B28AC2: start_thread (pthread_create.c:442)
|   |                                 ->87.93% (201,326,592B) 0x4BB9A03: clone (clone.S:100)
|   |
|   ->07.33% (16,785,408B) 0x4B29706: allocate_stack (allocatestack.c:370)
|     ->07.33% (16,785,408B) 0x4B29706: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
|       ->07.33% (16,785,408B) 0x4924328: std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
|         ->07.33% (16,785,408B) 0x109A15: std::thread::thread<void (&)(), , void>(void (&)()) (std_thread.h:143)
|           ->07.33% (16,785,408B) 0x109498: main (main.cpp:28)
|
->04.56% (10,432,512B) 0x4026CB7: __mmap64 (mmap64.c:58)
| ->04.56% (10,432,512B) 0x4026CB7: mmap (mmap64.c:46)
|   ->02.46% (5,640,192B) 0x4008F1B: _dl_map_segment (dl-map-segments.h:29)
|   | ->02.46% (5,640,192B) 0x4008F1B: _dl_map_segments (dl-map-segments.h:102)
|   |   ->02.46% (5,640,192B) 0x4008F1B: _dl_map_object_from_fd (dl-load.c:1258)
|   |     ->02.46% (5,640,192B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |       ->02.45% (5,619,712B) 0x4003494: openaux (dl-deps.c:64)
|   |       | ->02.45% (5,619,712B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |       |   ->02.45% (5,619,712B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |       |     ->02.45% (5,619,712B) 0x402339F: dl_main (rtld.c:1970)
|   |       |       ->02.45% (5,619,712B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |       |         ->02.45% (5,619,712B) 0x4021597: _dl_start_final (rtld.c:507)
|   |       |           ->02.45% (5,619,712B) 0x4021597: _dl_start (rtld.c:596)
|   |       |             ->02.45% (5,619,712B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |       |
|   |       ->00.01% (20,480B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->02.05% (4,698,112B) 0x4009098: _dl_map_segments (dl-map-segments.h:140)
|   | ->02.05% (4,698,112B) 0x4009098: _dl_map_object_from_fd (dl-load.c:1258)
|   |   ->02.05% (4,698,112B) 0x400A600: _dl_map_object (dl-load.c:2327)
|   |     ->02.04% (4,681,728B) 0x4003494: openaux (dl-deps.c:64)
|   |     | ->02.04% (4,681,728B) 0x401D349: _dl_catch_exception (dl-error-skeleton.c:208)
|   |     |   ->02.04% (4,681,728B) 0x4003C7B: _dl_map_object_deps (dl-deps.c:248)
|   |     |     ->02.04% (4,681,728B) 0x402339F: dl_main (rtld.c:1970)
|   |     |       ->02.04% (4,681,728B) 0x401F83B: _dl_sysdep_start (dl-sysdep.c:256)
|   |     |         ->02.04% (4,681,728B) 0x4021597: _dl_start_final (rtld.c:507)
|   |     |           ->02.04% (4,681,728B) 0x4021597: _dl_start (rtld.c:596)
|   |     |             ->02.04% (4,681,728B) 0x4020297: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
|   |     |
|   |     ->00.01% (16,384B) in 1+ places, all below ms_print's threshold (01.00%)
|   |
|   ->00.04% (94,208B) in 1+ places, all below ms_print's threshold (01.00%)
|
->00.19% (425,984B) in 1+ places, all below ms_print's threshold (01.00%)
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 53      2,308,909      228,966,400      228,966,400             0            0
Posted
Updated 4-Jul-24 7:01am
v5

It's likely that the difference is because the heap memory allocation was pretty full before you asked for the extra bytes - so the heap manager asked the system for another block of memory and got given 72MB. That may be a default allocation by the system, or the compiler / heap manager which may be very different from that used on the Windows version.

Allocation on the stack will not increase memory usage - the whole stack is allocated when the process is created and cannot be increased after that. It's also a load smaller than the heap, and automatically discards variables when the method exits for any reason - so unless you only need the memory for the lifetime of the method, you should use the heap instead (returning a pointer to stack memory is called a "dangling reference" and it can cause some really nasty intermittent errors which can be a total PITA to locate let alone rectify).
 
Share this answer
 
Comments
dilsdgr8 4-Jul-24 6:45am    
But this behavior is always observed. I allocate only 400 bytes. But 72mb increased in Virutal memory
Dave Kreskowiak 4-Jul-24 10:45am    
It seems you have to go read 'Griffs first paragraph again.
merano99 6-Jul-24 19:06pm    
The claim that "the whole stack is allocated when the process is created and cannot be increased after that." is not entirely correct in this form. Under Linux, the stack size of a process can be changed with the ulimit command, for example. If the stack is set to "unlimited", this means that the stack theoretically no longer has a fixed upper limit. The process can use as much memory for the stack as the system can provide without being restricted by an artificially set limit.
OriginalGriff 7-Jul-24 0:37am    
It was a generalisation - it's nearly universally considered a bad idea to stick too much stuff on the stack! :D
You allocating your 100 integers in a thread. When you start a new thread, the OS also allocates TLS (thread local storage) and Thread Context cache in addition to your new operator allocations.

If interested here is the Windows CONTEXT structure, I am sure Linux has similar:
CONTEXT (winnt.h) - Win32 apps | Microsoft Learn[^]

It is in essence a copy of a CPU image pushed into the heap until your thread time slice arrives and then read back by the OS into the processor. So it goes back and forth
 
Share this answer
 
v5
Comments
Malcolm Arthur McLean 7-Jul-24 8:15am    
It needs to store the register file to implement threading. But that would only be a few bytes. Unless it is doing some higher-level cacheing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900