Thread: memory management

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    memory management

    hello
    i have got some questions.
    int *pointer = (int *) malloc ( 12 );
    malloc reserves 12 Bytes on my Heap. I have once heard that malloc always reserves one pagesize. Is this true?

    another question.
    i have got a process which contains of a stack and heap and code segment.
    If my computer supports paging , for example the heap is divided into pages with a page size. what happens if malloc tries to reserve more memory on the heap and than the heap is too small. Do they really replace some sites.
    i have got the same understanding problem in segmentation
    thanks in advance.
    sry for my terrible english but these questions are really important
    Last edited by hansigarten2; 08-16-2010 at 09:55 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > malloc reserves 12 Bytes on my Heap. I have once heard that malloc always reserves one pagesize. Is this true?
    What is true is the program will ask the OS for a block of memory which is a multiple of pagesize. This is expensive (in time), so it usually asks for a reasonably sized block.

    As for the 12 bytes, it's likely to be more
    - padding to preserve alignment for other malloc calls
    - padding for memory pool management
    - padding for extra debug information
    None of these things you can know for sure from within your program.
    But it's not likely to be anywhere near a whole page.

    As for the rest,
    Working set - Wikipedia, the free encyclopedia
    and follow on to page replacement algorithms.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I believe the answer is going to depend on the operating system / platform you are using.

    For instance, on an IBM mainframe, the operating system will allocate a whole page frame (4K) to you, because it cheaper for the OP SYS to keep track of who has what allocated, but from your perspective, you get 12 bytes. A subsequent heap request, assuming the size needed is <= (4K-12), will most likely be allocated from the same page frame you already own.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    thanks 4 help until now!

    1. am i on the right track if i think that for example i allocate 5mb with malloc but i have got a 2 mb heap. Is page replacement the soltution for the problem? or do i have to change heap size?

    2. if i combine segmentation with paging does the memory has to be subsequent in ram? can the size of the segment vary? for example there is a process after my running process?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is page replacement the soltution for the problem? or do i have to change heap size?
    Are you writing a compiler run-time library or an operating system?

    If not, then the process is entirely transparent to you.
    You might have 5MB allocated, but there is NO way for you to know how much of it is physically mapped in RAM at any given moment (it may be none of it). All that matters is that when you try to access any of it, the OS will make sure the memory is there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by hansigarten2 View Post
    hello
    i have got some questions.
    int *pointer = (int *) malloc ( 12 );
    malloc reserves 12 Bytes on my Heap. I have once heard that malloc always reserves one pagesize. Is this true?
    The memory allocated depends on the request size, but yes the minimum allocated unit is one page.
    Quote Originally Posted by hansigarten2 View Post
    another question.
    i have got a process which contains of a stack and heap and code segment.
    If my computer supports paging , for example the heap is divided into pages with a page size. what happens if malloc tries to reserve more memory on the heap and than the heap is too small.
    On most *nixes there is a kernel tunable parameter that allows expansion of the data segment size upto a maximum allowable hard limit.
    If at the hard limit then nothing can be done unless the free list managed by malloc() has space, else the process abends with ENOMEM.
    Quote Originally Posted by hansigarten2 View Post
    Do they really replace some sites.
    Not sure what you're trying to get at?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by hansigarten2 View Post
    1. am i on the right track if i think that for example i allocate 5mb with malloc but i have got a 2 mb heap. Is page replacement the soltution for the problem? or do i have to change heap size?
    Page replacement is done by the OS; malloc() maintains its own free list and allocates chunks from it as and when requested.
    The list of free blocks malloc() maintains aren't returned to the OS until the process ends so page replacement doesn't come into play.
    All the more reason to free() what you have malloc()'d.
    And yes, the heap size can be bumped up as long as you haven't hit the hard limit for the data segment size, otherwise ENOMEM.
    Quote Originally Posted by hansigarten2 View Post
    2. if i combine segmentation with paging does the memory has to be subsequent in ram?
    Contiguous? Logically but not physically.
    Quote Originally Posted by hansigarten2 View Post
    can the size of the segment vary?
    Yep! since the segment table has a limit field which specifies the length of the segment.
    Quote Originally Posted by hansigarten2 View Post
    for example there is a process after my running process?
    ??

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    so i think i have understood it. let me summarize
    my understanding problem was always, because i taught that malloc reserves directly memory. but between malloc and ram is the memory allocation system. this system looks after pages! page replacement is managed by this system. so malloc has nothing to do with this memory allocation system. i always taught if there is to less memory available page replacement comes into action and replaces some pages. but this is managed by malloc.
    i hope i have said everything correctly. please correct me if am on the wrong track.

  9. #9
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    (quote)

    Code:
    int *pointer = (int *) malloc ( 12 );
    malloc reserves 12 Bytes on my Heap. I have once heard that malloc always reserves one pagesize. Is this true?
    It's my understanding that when using the malloc function, you are ASKING for memory resources, as opposed to "telling" the system. Worried that you wont have enough memory?

    When invoking malloc(), it dynamically allocates memory (or trys to), but the function itself also returns a value of NULL if it failed.

    Also for portability (maybe that's not a concern here) I would never use a literal in calling malloc; I always feel safer using the sizeof() thingy.

    Here's a re-write sample:

    Code:
    int *pointer = (int  *)  malloc ( 12 * sizeof(int) );    // Ask for 12 bytes of size int
    
    if ( pointer == NULL)     // check if successful
    {
         puts("Out of HUNK!")  // hehehe... remember that one from the old DOS days?
    
         return 1;
    }
    
         puts(" You have 12 bytes of type int reserved.\n")
    Hope this works for you & good luck!
    Last edited by Char*Pntr; 08-17-2010 at 07:23 PM. Reason: typo... returns NULL if fail...

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    thank you for your efforts

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by hansigarten2 View Post
    so i think i have understood it. let me summarize
    my understanding problem was always, because i taught that malloc reserves directly memory. but between malloc and ram is the memory allocation system.
    That "memory allocation system" is popularly known as the OS.
    Quote Originally Posted by hansigarten2 View Post
    this system looks after pages! page replacement is managed by this system. so malloc has nothing to do with this memory allocation system.
    malloc() has everything to do with the OS. The very first call of malloc() requests memory from the OS and whenever a dynamic storage request can't be satisfied, malloc() attempts to obtain more memory from the OS.
    Quote Originally Posted by hansigarten2 View Post
    i always taught if there is to less memory available page replacement comes into action and replaces some pages. but this is managed by malloc.
    i hope i have said everything correctly. please correct me if am on the wrong track.
    Yep! that's correct, the OS is responsible for paging out parts of a process if there is shortage of memory but it doesn't touch the free list managed by malloc().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory management woes
    By homer_3 in forum C Programming
    Replies: 3
    Last Post: 02-16-2010, 02:29 PM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  3. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  4. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM