reg dynamic memory allocation - Unix

This is a discussion on reg dynamic memory allocation - Unix ; Hi Everyone, I have a query regarding dynamic memory allocation, I request for allocation of 100 kb of memory at runtime using malloc() and if there isn't enough space in the random access memory, the mallooc fails. But wouldn't it ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: reg dynamic memory allocation

  1. reg dynamic memory allocation

    Hi Everyone,

    I have a query regarding dynamic memory allocation,

    I request for allocation of 100 kb of memory at runtime using
    malloc() and if there isn't enough space in the random access memory,
    the mallooc fails.

    But wouldn't it have been better to have the allocation done,
    specially when virtual memory is available via pages.

    Can anyone clarify this scenario?

    Thanks in advance!!!


  2. Re: reg dynamic memory allocation

    sam_cit@yahoo.co.in wrote:
    > I have a query regarding dynamic memory allocation,


    > I request for allocation of 100 kb of memory at runtime using
    > malloc() and if there isn't enough space in the random access memory,
    > the mallooc fails.


    No.

    > But wouldn't it have been better to have the allocation done,
    > specially when virtual memory is available via pages.


    Yes, that's why it's done that way.


  3. Re: reg dynamic memory allocation

    > > I have a query regarding dynamic memory allocation,
    > > I request for allocation of 100 kb of memory at runtime using
    > > malloc() and if there isn't enough space in the random access memory,
    > > the mallooc fails.

    >
    > No.
    >


    Do you know in what other cases does malloc() returns NULL ?


  4. Re: reg dynamic memory allocation

    sam_cit@yahoo.co.in schrieb:
    >>> I have a query regarding dynamic memory allocation,
    >>> I request for allocation of 100 kb of memory at runtime using
    >>> malloc() and if there isn't enough space in the random access memory,
    >>> the mallooc fails.


    >> No.


    > Do you know in what other cases does malloc() returns NULL ?


    - If virtual memory is exhausted.

    - If you try to allove more memory than permitted for you ...
    see getrlimit() ... RLIMIT_AS ... RLIMIT_DATA.

    Note, that in absence of any swap device ... virtual memory
    is limited to real memory.





  5. Re: reg dynamic memory allocation

    On Apr 23, 7:15 am, Rainer Temme
    wrote:

    > > Do you know in what other cases does malloc() returns NULL ?

    >
    > - If virtual memory is exhausted.


    Note that this can either by system virtual memory (RAM plus swap) or
    process virtual memory (address space).

    DS


  6. Re: reg dynamic memory allocation

    >
    > Note that this can either by system virtual memory (RAM plus swap) or
    > process virtual memory (address space).
    >
    > DS


    Can you explain what do you mean by process virtual memory? Isn't
    that the swap file's contents?


  7. Re: reg dynamic memory allocation

    sam_cit@yahoo.co.in wrote:
    >> Note that this can either by system virtual memory (RAM plus swap) or
    >> process virtual memory (address space).


    > Can you explain what do you mean by process virtual memory? Isn't
    > that the swap file's contents?


    Imagine a system with 4GB ram, 12GB swap.

    In this system the limits per process are such that
    a single process cannot get more than 4GB virtual memory
    (due to the 32bit nature of the system).

    Now the systems virtual memory is 16GB, the process virtual
    memory is 'only' 4GB. If the process has already exhausted
    his virtual address spaces, the system cannot add any
    more virtual address space to this process even if there
    is more space available to the system (and to other processes).

    Also...
    The limits (getrlimit) of what a process is allowed to allocate
    can be much lower. If the process has reached its limit it
    will not get any more memory (even the system has more memory
    available).



  8. Re: reg dynamic memory allocation

    On Apr 24, 2:20 am, sam_...@yahoo.co.in wrote:

    > Can you explain what do you mean by process virtual memory? Isn't
    > that the swap file's contents?


    Umm, no. Every process has its own view of memory. The executable
    itself is mapped into some address, any memory it allocates is mapped
    in, libraries are mapped in, and so on. Those mappings create a
    virtual address space for that process.

    To put it in simplest terms, suppose 'malloc' returns a 32-bit
    pointer. In that case, you cannot possibly 'malloc' more than 2^32
    bytes at once because there would be enough pointer values.

    DS


  9. Re: reg dynamic memory allocation

    On Apr 23, 9:29 pm, Rainer Temme
    wrote:
    > sam_...@yahoo.co.in wrote:
    > > I have a query regarding dynamic memory allocation,
    > > I request for allocation of 100 kb of memory at runtime using
    > > malloc() and if there isn't enough space in the random access memory,
    > > the mallooc fails.

    >
    > No.
    >
    > > But wouldn't it have been better to have the allocation done,
    > > specially when virtual memory is available via pages.

    >
    > Yes, that's why it's done that way.


    But POSIX doesn't say anything about the malloc() is a blocking call,
    is it portable to rely malloc() but not try malloc again immediately?

    http://www.opengroup.org/onlinepubs/...ns/malloc.html


  10. Re: reg dynamic memory allocation

    In article <1177564273.444549.48870@c18g2000prb.googlegroups.c om>,
    Bin Chen wrote:

    > On Apr 23, 9:29 pm, Rainer Temme
    > wrote:
    > > sam_...@yahoo.co.in wrote:
    > > > I have a query regarding dynamic memory allocation,
    > > > I request for allocation of 100 kb of memory at runtime using
    > > > malloc() and if there isn't enough space in the random access memory,
    > > > the mallooc fails.

    > >
    > > No.
    > >
    > > > But wouldn't it have been better to have the allocation done,
    > > > specially when virtual memory is available via pages.

    > >
    > > Yes, that's why it's done that way.

    >
    > But POSIX doesn't say anything about the malloc() is a blocking call,
    > is it portable to rely malloc() but not try malloc again immediately?


    You could try again, but it's not likely to improve. Most of the time
    the reason for malloc failure is that the contiguous space isn't
    available in your process's heap. It's possible that it's because
    system-wide VM is exhausted, and some other process might return space
    before you try again, but it's not very likely.

    Also, some versions of Unix use "optimistic" VM allocation. When a
    process requests additional VM, it always succeeds (if the process has
    the virtual addresses available). Swap space is not reserved
    immediately, it's only allocated as needed when any of the new pages are
    modified. If no swap space is available, the process gets a signal.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

+ Reply to Thread