reg dynamic memory allocation
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > reg dynamic memory allocation




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    reg dynamic memory allocation  
sam_cit@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-07 06: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!!!






[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-07 06: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.






[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
sam_cit@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-07 06:23 PM

> >  I have a query regarding dynamic memory allocation, 
>
> No.
>

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






[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-23-07 06: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.









[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-24-07 12:21 PM

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






[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
sam_cit@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-24-07 12:21 PM

>
> 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?






[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-24-07 12:21 PM

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).







[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-24-07 12:21 PM

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






[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
Bin Chen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-26-07 06: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






[ Post a follow-up to this message ]



    Re: reg dynamic memory allocation  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-07 06: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 ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:29 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register