0

I came across one situation while trying to debug one program for segmentation fault. I found the program is able to allocate 4GB of dynamic memory while the available memory in the /proc/meminfo is only 1GB. Please find below the program and the my system RAM info.

program:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char *buf;
    buf=malloc(1<<31);
    fgets(buf,1024,stdin);
    printf("%s\n",buf);
    //return 1;
}

#vim /proc/meminfo

MemTotal:      1032984 kB
MemFree:        743620 kB
Buffers:         54156 kB
Cached:         175424 kB
SwapCached:          0 kB
Active:          91436 kB
Inactive:       158196 kB
HighTotal:      129440 kB
HighFree:          252 kB
LowTotal:       903544 kB
LowFree:        743368 kB
SwapTotal:    10241428 kB
SwapFree:     10241428 kB
Dirty:               0 kB
Writeback:           0 kB
Mapped:          32740 kB
Slab:            25748 kB
CommitLimit:  10757920 kB
Committed_AS:    74952 kB
PageTables:       1316 kB
VmallocTotal:   106488 kB
VmallocUsed:      7004 kB
VmallocChunk:    99300 kB 

This one is really confusing me. Kindly suggest me.

Chandru
  • 1,306
  • 13
  • 21
SKM
  • 29
  • 2
  • 4
  • @HristoIliev: Except `malloc` commits the memory too. – user541686 Jul 11 '14 at 12:09
  • Virtual memory springs to mind – Ed Heal Jul 11 '14 at 12:09
  • @Mehrdad, not always true. Huge allocations like the one in that sample program are done using anonymous `mmap` and are not pre-committed. – Hristo Iliev Jul 11 '14 at 12:10
  • @HristoIliev: I guess it depends on what exactly you call "committing" memory? Linux over-commits, that means it "commits" more memory than the system has, and correct me if I'm wrong but that's exactly the problem we're seeing here, no? – user541686 Jul 11 '14 at 12:12
  • @HristoIliev: With your definition of commitment, "over-commitment" is impossible to define, yet it's a very common and well-understood term. – user541686 Jul 11 '14 at 12:17
  • This has nothing to do with overcommit and has been incorrectly marked as duplicate. /proc/meminfo clearly says that there is 10GB swap. No overcommit has happened. – Art Jul 11 '14 at 12:41
  • Also, the comments here are just confused about the word "commit". "Commit" here means the same thing as in the dictionary - promise. Over-commit means that the system has promised more than it can deliver. In this case it can clearly deliver 2GB of memory even though it might be quite slow because it would be swapping. – Art Jul 11 '14 at 12:48
  • I agree with Art that the question has been wrongly closed as duplicate. Besides, I've used "committed" where I meant "backed by a physical memory store" and that's incorrect. I've deleted those comments to prevent confusion. – Hristo Iliev Jul 11 '14 at 13:27

0 Answers0