skip to Main Content

Having this:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define GB (1<<30)
#define N 10000L //number of virtual blocks(arrays)

int main (void) {

    int *ar[N]; 
    for(int i =0; i<N ; i++)
    {
        ar[i]=malloc(GB); //alloc virtually one GB
        if(!ar[i]){
            printf("done at %in",i);
            return 0;
        }
    }
    printf("allocated %li blocksn",N);

    for(int i =0; i<N ; i++)
    {
        memset(ar[i],2,GB); // get physical gigs for that array
        printf("wrote to %i GBn",i+1);
    }


    return 0;
}

But I will not get even one:

allocated 10000 blocks //virtual memory (one block==1GB)
wrote to 1 GB //real memory (not really

Command terminated

Press ENTER or type command to continue

I know I have at leaset 4 gigs on my machine and I also know OS has some limit it needs to operate, however I cannot get even 2 GB for user space? That is strange. Can someone explain please?

$uname -a:
`4.19.0-9-amd64 #1 SMP Debian 4.19.118-2 (2020-04-29) x86_64 GNU/Linux

EDIT:
It gets stranger:
Now if I run (after an hour from the previous output):

allocated 10000 blocks
wrote to 1 GB
wrote to 2 GB

Command terminated

Press ENTER or type command to continue

so it gets through 2 time in for loop, and allocates 2GB of real memory, but before an hour, I catch only 1GB and something (not full 2GB).

addition info:
$free -h:

    total        used        free      shared  buff/cache   available
Mem:          3.7Gi       1.1Gi       2.2Gi       236Mi       404Mi       2.2Gi
Swap:            0B          0B          0B

How can I take advantage from these information? So it is I said -> I have 4GB (total), but can get 2GB. That is maximum for user space?

2

Answers


  1. Note: This answer does not solve the problem OP was having because it’s based on fake code originally in the question, and still present in the question after the first request to correct fake code was made and acted upon. It does explain the question as originally asked.

    If the code you posted is accurate now, your problem is that you are attempting to call functions without valid declarations for them (I see no #include directives). This is invalid C and the compiler should warn (or ideally error out) if you do it. Always add -Werror=implicit-function-declaration to get it to do that.

    The particular mechanism of your crash is likely this: memset takes a size_t (unsigned long) as its third argument, but without a prototype, the function is being called as if the type of the function matched the argument types you provided (subject to default promotions). This produces undefined behavior. 1<<30 has type int, not unsigned long, and on x86_64 ABI, int is passed in the low 32 bits of a 64-bit register, with arbitrary junk allowed in the upper bits. So rather than passing 1 GB to memset, you’re passing some astronomically large number.

    Login or Signup to reply.
  2. Possibly a memory or swap limitation.

    On a 4GB Ubuntu 18 VM, I get:

    $ gcc -o alloc -Wall alloc.c
    $ free -h
                  total        used        free      shared  buff/cache   available
    Mem:           3.9G        154M        3.1G         23M        572M        3.5G
    Swap:          2.0G          0B        2.0G
    $ uname -a
    Linux ub18 4.15.0-101-generic #102-Ubuntu SMP Mon May 11 10:07:26 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
    $./alloc
    allocated 10000 blocks
    wrote to 1 GB
    wrote to 2 GB
    wrote to 3 GB
    wrote to 4 GB
    wrote to 5 GB
    Killed
    $ free -h
                  total        used        free      shared  buff/cache   available
    Mem:           3.9G        127M        3.7G        1.6M         60M        3.6G
    Swap:          2.0G        112M        1.9G
    $ 
    

    syslog says:

    May 30 17:57:56 ub18 kernel: [  240.723065] alloc invoked oom-killer: gfp_mask=0x14280ca(GFP_HIGHUSER_MOVABLE|__GFP_ZERO), nodemask=(null), order=0, oom_score_adj=0
    May 30 17:57:56 ub18 kernel: [  240.723067] alloc cpuset=/ mems_allowed=0
    May 30 17:57:56 ub18 kernel: [  240.723072] CPU: 0 PID: 3062 Comm: alloc Not tainted 4.15.0-101-generic #102-Ubuntu
    May 30 17:57:56 ub18 kernel: [  240.723072] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
    May 30 17:57:56 ub18 kernel: [  240.723073] Call Trace:
    May 30 17:57:56 ub18 kernel: [  240.723088]  dump_stack+0x6d/0x8e
    May 30 17:57:56 ub18 kernel: [  240.723094]  dump_header+0x71/0x285
    May 30 17:57:56 ub18 kernel: [  240.723096]  oom_kill_process+0x21f/0x420
    May 30 17:57:56 ub18 kernel: [  240.723097]  out_of_memory+0x116/0x4e0
    May 30 17:57:56 ub18 kernel: [  240.723100]  __alloc_pages_slowpath+0xa53/0xe00
    May 30 17:57:56 ub18 kernel: [  240.723102]  __alloc_pages_nodemask+0x29a/0x2c0
    May 30 17:57:56 ub18 kernel: [  240.723104]  alloc_pages_vma+0x88/0x1f0
    May 30 17:57:56 ub18 kernel: [  240.723107]  __handle_mm_fault+0x8b7/0x1290
    May 30 17:57:56 ub18 kernel: [  240.723109]  handle_mm_fault+0xb1/0x210
    May 30 17:57:56 ub18 kernel: [  240.723112]  __do_page_fault+0x281/0x4b0
    May 30 17:57:56 ub18 kernel: [  240.723114]  do_page_fault+0x2e/0xe0
    May 30 17:57:56 ub18 kernel: [  240.723115]  ? page_fault+0x2f/0x50
    May 30 17:57:56 ub18 kernel: [  240.723116]  page_fault+0x45/0x50
    May 30 17:57:56 ub18 kernel: [  240.723118] RIP: 0033:0x7f6d13bd7963
    May 30 17:57:56 ub18 kernel: [  240.723119] RSP: 002b:00007ffeede7e608 EFLAGS: 00010206
    May 30 17:57:56 ub18 kernel: [  240.723120] RAX: 00007f6b93b16010 RBX: 0000000000000000 RCX: 00007f6bb0993000
    May 30 17:57:56 ub18 kernel: [  240.723121] RDX: 00007f6bd3b16000 RSI: 0000000000000002 RDI: 00007f6b93b16010
    May 30 17:57:56 ub18 kernel: [  240.723122] RBP: 00007ffeede91eb0 R08: 0000000000000000 R09: 0000000000000000
    May 30 17:57:56 ub18 kernel: [  240.723122] R10: 0000000000000000 R11: 0000000000000246 R12: 0000559df7b44640
    May 30 17:57:56 ub18 kernel: [  240.723123] R13: 00007ffeede91f90 R14: 0000000000000000 R15: 0000000000000000
    May 30 17:57:56 ub18 kernel: [  240.723124] Mem-Info:
    May 30 17:57:56 ub18 kernel: [  240.723127] active_anon:780631 inactive_anon:158435 isolated_anon:0
    May 30 17:57:56 ub18 kernel: [  240.723127]  active_file:13 inactive_file:10 isolated_file:1
    May 30 17:57:56 ub18 kernel: [  240.723127]  unevictable:0 dirty:0 writeback:2 unstable:0
    May 30 17:57:56 ub18 kernel: [  240.723127]  slab_reclaimable:4568 slab_unreclaimable:7013
    May 30 17:57:56 ub18 kernel: [  240.723127]  mapped:196 shmem:364 pagetables:15033 bounce:0
    May 30 17:57:56 ub18 kernel: [  240.723127]  free:21182 free_pcp:60 free_cma:0
    May 30 17:57:56 ub18 kernel: [  240.723130] Node 0 active_anon:3122524kB inactive_anon:633740kB active_file:52kB inactive_file:40kB unevictable:0kB isolated(anon):0kB isolated(file):4kB mapped:784kB dirty:0kB writeback:8kB shmem:1456kB shmem_thp: 0kB shmem_pmdmapped: 0kB anon_thp: 0kB writeback_tmp:0kB unstable:0kB all_unreclaimable? no
    May 30 17:57:56 ub18 kernel: [  240.723131] Node 0 DMA free:15736kB min:268kB low:332kB high:396kB active_anon:168kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB writepending:0kB present:15992kB managed:15908kB mlocked:0kB kernel_stack:0kB pagetables:0kB bounce:0kB free_pcp:0kB local_pcp:0kB free_cma:0kB
    May 30 17:57:56 ub18 kernel: [  240.723133] lowmem_reserve[]: 0 3421 3867 3867 3867
    May 30 17:57:56 ub18 kernel: [  240.723135] Node 0 DMA32 free:61252kB min:59536kB low:74420kB high:89304kB active_anon:2779728kB inactive_anon:629972kB active_file:20kB inactive_file:24kB unevictable:0kB writepending:48kB present:3653568kB managed:3566076kB mlocked:0kB kernel_stack:80kB pagetables:51412kB bounce:0kB free_pcp:120kB local_pcp:120kB free_cma:0kB
    May 30 17:57:56 ub18 kernel: [  240.723138] lowmem_reserve[]: 0 0 446 446 446
    May 30 17:57:56 ub18 kernel: [  240.723139] Node 0 Normal free:7740kB min:7772kB low:9712kB high:11652kB active_anon:342620kB inactive_anon:3748kB active_file:28kB inactive_file:8kB unevictable:0kB writepending:0kB present:524288kB managed:457424kB mlocked:0kB kernel_stack:2096kB pagetables:8720kB bounce:0kB free_pcp:120kB local_pcp:120kB free_cma:0kB
    May 30 17:57:56 ub18 kernel: [  240.723142] lowmem_reserve[]: 0 0 0 0 0
    May 30 17:57:56 ub18 kernel: [  240.723143] Node 0 DMA: 0*4kB 1*8kB (M) 1*16kB (M) 1*32kB (U) 3*64kB (UM) 1*128kB (U) 2*256kB (UM) 1*512kB (M) 2*1024kB (UM) 0*2048kB 3*4096kB (M) = 15736kB
    May 30 17:57:56 ub18 kernel: [  240.723149] Node 0 DMA32: 439*4kB (UME) 357*8kB (UME) 202*16kB (UME) 103*32kB (UE) 37*64kB (UME) 15*128kB (UME) 13*256kB (UME) 9*512kB (UE) 1*1024kB (E) 0*2048kB 9*4096kB (M) = 61252kB
    May 30 17:57:56 ub18 kernel: [  240.723156] Node 0 Normal: 621*4kB (UMEH) 285*8kB (UMEH) 110*16kB (UMEH) 34*32kB (UEH) 2*64kB (MH) 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 7740kB
    May 30 17:57:57 ub18 kernel: [  240.723162] Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=2048kB
    May 30 17:57:57 ub18 kernel: [  240.723162] 3052 total pagecache pages
    May 30 17:57:57 ub18 kernel: [  240.723164] 2652 pages in swap cache
    May 30 17:57:57 ub18 kernel: [  240.723168] Swap cache stats: add 527798, delete 525146, find 859/1742
    May 30 17:57:57 ub18 kernel: [  240.723168] Free swap  = 0kB
    May 30 17:57:57 ub18 kernel: [  240.723169] Total swap = 2104316kB
    May 30 17:57:57 ub18 kernel: [  240.723169] 1048462 pages RAM
    May 30 17:57:57 ub18 kernel: [  240.723170] 0 pages HighMem/MovableOnly
    May 30 17:57:57 ub18 kernel: [  240.723170] 38610 pages reserved
    May 30 17:57:57 ub18 kernel: [  240.723171] 0 pages cma reserved
    May 30 17:57:57 ub18 kernel: [  240.723171] 0 pages hwpoisoned
    May 30 17:57:57 ub18 kernel: [  240.723172] [ pid ]   uid  tgid total_vm      rss pgtables_bytes swapents oom_score_adj name
    May 30 17:57:57 ub18 kernel: [  240.723175] [  396]     0   396    23715       11   217088      164             0 systemd-journal
    May 30 17:57:57 ub18 kernel: [  240.723177] [  417]     0   417    24427        0    90112       51             0 lvmetad
    May 30 17:57:57 ub18 kernel: [  240.723178] [  418]     0   418    11706       43   118784      590         -1000 systemd-udevd
    May 30 17:57:57 ub18 kernel: [  240.723179] [  586] 62583   586    35483        0   184320      151             0 systemd-timesyn
    May 30 17:57:57 ub18 kernel: [  240.723181] [  755]   100   755    20044        8   176128      176             0 systemd-network
    May 30 17:57:57 ub18 kernel: [  240.723182] [  782]   101   782    17659       10   172032      157             0 systemd-resolve
    May 30 17:57:57 ub18 kernel: [  240.723183] [  957]     0   957    17650        8   180224      172             0 systemd-logind
    May 30 17:57:57 ub18 kernel: [  240.723184] [  959]     0   959    71564       14   200704      214             0 accounts-daemon
    May 30 17:57:57 ub18 kernel: [  240.723185] [  961]   102   961    65760        0   163840      284             0 rsyslogd
    May 30 17:57:57 ub18 kernel: [  240.723186] [  962]     0   962     7083        3   102400       49             0 atd
    May 30 17:57:57 ub18 kernel: [  240.723188] [  963]     0   963     7507        0   102400       75             0 cron
    May 30 17:57:57 ub18 kernel: [  240.723189] [  996]     0   996    42276      102   217088     1841             0 networkd-dispat
    May 30 17:57:57 ub18 kernel: [  240.723190] [  997]     0   997   160115      231   233472     4850          -900 snapd
    May 30 17:57:57 ub18 kernel: [  240.723191] [ 1008]     0  1008    23885        0    77824       73             0 lxcfs
    May 30 17:57:57 ub18 kernel: [  240.723193] [ 1012]   103  1012    12571        1   139264      202          -900 dbus-daemon
    May 30 17:57:57 ub18 kernel: [  240.723194] [ 1094]     0  1094   101403       29   401408      641             0 NetworkManager
    May 30 17:57:57 ub18 kernel: [  240.723195] [ 1095]     0  1095    11308        6   122880      131             0 wpa_supplicant
    May 30 17:57:57 ub18 kernel: [  240.723196] [ 1110]     0  1110   108581       10   356352      344             0 ModemManager
    May 30 17:57:57 ub18 kernel: [  240.723197] [ 1167]     0  1167    72221        0   212992      208             0 polkitd
    May 30 17:57:57 ub18 kernel: [  240.723198] [ 1283]     0  1283    46486       96   249856     1879             0 unattended-upgr
    May 30 17:57:57 ub18 kernel: [  240.723200] [ 1450]     0  1450     3722        0    69632       33             0 agetty
    May 30 17:57:57 ub18 kernel: [  240.723201] [ 1531]     0  1531    18075       11   184320      177         -1000 sshd
    May 30 17:57:57 ub18 kernel: [  240.723202] [ 1571]   111  1571    79693       19   331776      484          -900 postgres
    May 30 17:57:57 ub18 kernel: [  240.723203] [ 1578]   111  1578    79781        4   335872      500          -900 postgres
    May 30 17:57:57 ub18 kernel: [  240.723204] [ 1603]   111  1603    79781        1   294912      500             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723205] [ 1604]   111  1604    79781       10   311296      500             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723207] [ 1605]   111  1605    79781        2   303104      507             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723208] [ 1606]   111  1606    79883       97   311296      521             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723209] [ 1607]   111  1607    43543        7   282624      509             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723210] [ 1608]   111  1608    79882       58   303104      528             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723211] [ 1611]   111  1611    79693        0   299008      501             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723212] [ 1612]   111  1612    79693       19   311296      495             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723213] [ 1613]   111  1613    79693        0   307200      506             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723214] [ 1614]   111  1614    79794       71   315392      530             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723216] [ 1615]   111  1615    43457       13   282624      511             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723217] [ 1616]   111  1616    79771       46   307200      532             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723218] [ 2896]     0  2896    26997        7   249856      245             0 sshd
    May 30 17:57:57 ub18 kernel: [  240.723219] [ 2908]  1000  2908    19166        0   180224      277             0 systemd
    May 30 17:57:57 ub18 kernel: [  240.723220] [ 2909]  1000  2909    48429        0   266240      593             0 (sd-pam)
    May 30 17:57:57 ub18 kernel: [  240.723221] [ 3026]  1000  3026    26997       11   245760      243             0 sshd
    May 30 17:57:57 ub18 kernel: [  240.723223] [ 3027]  1000  3027     5823        1    81920      461             0 bash
    May 30 17:57:57 ub18 kernel: [  240.723224] [ 3062]  1000  3062 2621451127   934324 93466624   504770             0 alloc
    May 30 17:57:57 ub18 kernel: [  240.723225] [ 3089]   111  3089    79921      271   319488      468             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723226] [ 3090]   111  3090    79921      331   319488      415             0 postgres
    May 30 17:57:57 ub18 kernel: [  240.723227] [ 3091]     0  3091     8646       38    98304        0             0 sshd
    May 30 17:57:57 ub18 kernel: [  240.723228] Out of memory: Kill process 3062 (alloc) score 951 or sacrifice child
    May 30 17:57:57 ub18 kernel: [  240.723268] Killed process 3062 (alloc) total-vm:10485804508kB, anon-rss:3737288kB, file-rss:8kB, shmem-rss:0kB
    May 30 17:57:57 ub18 kernel: [  240.916591] oom_reaper: reaped process 3062 (alloc), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB
    $ 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search