Jim Bauer wrote:
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.
Why additional memory wont be allocated from the kernel?
Is it because here in this case i havent used the memory assigned to a1?
But what i tried later is that i stored 20 numbers starting from
location pointed by a1.
It'll satisify that request for unused memory from previous allocations.
What will be the usused memory in this case?
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.
Yes I have read the page from the location:
http://www.opengroup.org/onlinepubs/009695399/functions/getrlimit.html
and it says
"RLIMIT_AS
This is the maximum size of a process' total available memory, in
bytes. If this limit is exceeded, the /malloc/()
<http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html>
and /mmap/()
<http://www.opengroup.org/onlinepubs/009695399/functions/mmap.html>
functions shall fail with /errno/ set to [ENOMEM]. In addition, the
automatic stack growth fails with the effects outlined above."
malloc is clearly mentioned.
Disable the first malloc() and you'll likely see the malloc()
after the setrlimit() fail.
yes i agree with that but still ii couldnt get that in the later case
there is no unused memory available . so why memory allocation for b1
doesn't result in failure?
Regards
Shivali
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/