|
Home > Archive > Unix Programming > April 2007 > reg dynamic memory allocation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
reg dynamic memory allocation
|
|
| sam_cit@yahoo.co.in 2007-04-23, 1:23 pm |
| 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!!!
| |
| Rainer Temme 2007-04-23, 1:23 pm |
| 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.
| |
| sam_cit@yahoo.co.in 2007-04-23, 1:23 pm |
| > > I have a query regarding dynamic memory allocation,
>
> No.
>
Do you know in what other cases does malloc() returns NULL ?
| |
| Rainer Temme 2007-04-23, 1:23 pm |
| sam_cit@yahoo.co.in schrieb:
[vbcol=seagreen]
[vbcol=seagreen]
> 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.
| |
| David Schwartz 2007-04-24, 7:21 am |
| On Apr 23, 7:15 am, Rainer Temme <Rainer_Te...@nospam.hotmail_dot_com>
wrote:
>
> - If virtual memory is exhausted.
Note that this can either by system virtual memory (RAM plus swap) or
process virtual memory (address space).
DS
| |
| sam_cit@yahoo.co.in 2007-04-24, 7:21 am |
| >
> 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?
| |
| Rainer Temme 2007-04-24, 7:21 am |
| sam_cit@yahoo.co.in wrote:
[vbcol=seagreen]
> 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).
| |
| David Schwartz 2007-04-24, 7:21 am |
| 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
| |
| Bin Chen 2007-04-26, 1:18 am |
| On Apr 23, 9:29 pm, Rainer Temme <Rainer_Te...@nospam.hotmail_dot_com>
wrote:
> sam_...@yahoo.co.in wrote:
>
> No.
>
>
> 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...ons/malloc.html
| |
| Barry Margolin 2007-04-27, 1:17 am |
| In article <1177564273.444549.48870@c18g2000prb.googlegroups.com>,
Bin Chen <binary.chen@gmail.com> wrote:
> On Apr 23, 9:29 pm, Rainer Temme <Rainer_Te...@nospam.hotmail_dot_com>
> wrote:
>
> 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 ***
|
|
|
|
|