Re: setrlimit + malloc + RLIMIT_AS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



shivaligupta wrote:
> 
> Expected Output:
> Memory allocated
> RLIMIT_AS:
> 0
> 8
> 8
> Memory allocation failed for the first time
> 
> 
> Observed o/p:
> Memory allocated
> RLIMIT_AS:
> 0
> 8
> 8
> Memory allocated
> 
> Why this discrepancy???
> Why the second time malloc didnt fail?

Because no additional memory needed to be allocated from the kernel.
It'll satisify that request for unused memory from previous allocations.
malloc() isn't going to turn a request for 20*sizeof(int) bytes
into a brk() for 20*sizeof(int) additional bytes.  That would be
grossly inefficient.  Read the RLIMIT_AS section of the manpage for
setrlimit().  It say it "affects  calls  to brk(2), mmap(2) and
mremap(2)".  malloc(3) is not mentioned.

Disable the first malloc() and you'll likely see the malloc()
after the setrlimit() fail.


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux