Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: why its not consuming memory

Reply
Thread Tools

Re: why its not consuming memory

 
 
p_cricket_guy@yahoo.co.in
Guest
Posts: n/a
 
      05-24-2007
On May 23, 11:26 pm, Ravi <(E-Mail Removed)> wrote:
> I created a program just to amuse me. I consumes a lot of memory and
> never gives it up:
>
> #incluse <stdio.h>
> #include <stdlib.h>
> int main(void) {
> void *a;
> while(1)
> a = malloc(99999);
>
> }
>
> but the output of vmstat (on GNU/Linux) is a very constant as:
>
> procs -----------memory---------- ---swap-- -----io---- -system-- ----
> cpu----
> r b swpd free buff cache si so bi bo in cs us
> sy id wa
> 1 0 0 157292 51668 201972 0 0 97 23 379 408 10
> 8 80 2


< sinpped >

This is more of a Linux question. Folks in a Linux newsgroup would
provide a better answer.

<OT>
Try modifying your program to malloc memory of size equal to Virtual
Memory page size on your architecture. Also, write to at least one
character into the allocated memory in each iteration. You might
see what you expect.

/* Disclaimer: Use with caution - */

#include <stdio.h>
#include <stdlib.h>

#define PAGE_SIZE 4000 /* Depends on your arch */
int main(void) {
char *a;
size_t total = 0;
while(1) {
a = malloc(PAGE_SIZE);
if (a == NULL) {
printf("No more memory after %d bytes \n", total);
return EXIT_SUCCESS; /* Hope your OS frees all
the memory allocated ! */
}
total += PAGE_SIZE;
a[PAGE_SIZE/2] = 1;
}
return EXIT_SUCCESS;
}

OS's with virtual memory may actually allocate physical memory
when the allocated memory is accessed.

</OT>

 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-24-2007
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> On May 23, 11:26 pm, Ravi <(E-Mail Removed)> wrote:
>> I created a program just to amuse me. I consumes a lot of memory and
>> never gives it up:

> < sinpped >
>
> This is more of a Linux question. Folks in a Linux newsgroup would
> provide a better answer.
>
> <OT>
> Try modifying your program to malloc memory of size equal to Virtual
> Memory page size on your architecture. Also, write to at least one
> character into the allocated memory in each iteration. You might
> see what you expect.
>
> /* Disclaimer: Use with caution - */
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #define PAGE_SIZE 4000 /* Depends on your arch */

Indeed arch dependant, but almost certailiny not 4000, more likely a power
of 2 like 4096

Bye, Jojo


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
why its not consuming memory Ravi C Programming 7 05-25-2007 02:41 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments