Find the answer to your Linux question:
Results 1 to 5 of 5
Hi All, Have a few questions regarding malloc. 1. How does "malloc" system call attocate memory? 2. Are there any env variables that can affect the allocation of memory in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie rinjo's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore, India
    Posts
    228

    Question malloc


    Hi All,

    Have a few questions regarding malloc.

    1. How does "malloc" system call attocate memory?
    2. Are there any env variables that can affect the allocation of memory in malloc?
    3. Is fragmentation of heap and non availability of heap the only reasons for malloc failure?
    Linux Rocks!!!!
    -- Rinjo
    Setup: Oses: Windows 7 HB and Fedora 17 dual boot Hardware: HP Pavilion G6-2005AX laptop, AMD A8 Quad Core, 4 GB RAM, 1.5 GB dual Graphics card, 500GB HDD.

  2. #2
    Just Joined!
    Join Date
    Oct 2008
    Posts
    6
    write one line program that use malloc, and then use strace on it.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    55
    1. How does "malloc" system call attocate memory?
    -> kernel does not allocate memory as soon as you do malloc, but it only prepares vmas. and when you actually access it it allocates page because of page fault exception. if it was a read access then kernel allocates special page called zero page, which is called demand paging
    2. Are there any env variables that can affect the allocation of memory in malloc?
    I think no.
    3. Is fragmentation of heap and non availability of heap the only reasons for malloc failure?
    there is nothing like fragamentation of heap, because your process understands virtual address space,not physical. it is kernel who understands it.
    so for malloc process may sleep or other pages could be swapped out, if the swap area is full and as physical memory fragamented (internal fragmentation) then malloc may fail.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie rinjo's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore, India
    Posts
    228
    Hi Matt,

    Thanks for your detailed answer to my query.
    Linux Rocks!!!!
    -- Rinjo
    Setup: Oses: Windows 7 HB and Fedora 17 dual boot Hardware: HP Pavilion G6-2005AX laptop, AMD A8 Quad Core, 4 GB RAM, 1.5 GB dual Graphics card, 500GB HDD.

  6. #5
    Just Joined!
    Join Date
    Nov 2008
    Location
    Bangalore
    Posts
    4
    For detailed explanation on how malloc allocates memory visit these sites:
    CS360 Lecture notes -- Malloc Lecture #1
    CS360 Lecture notes -- Malloc Lecture #2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •