Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get total heap size of a process

ganesh.kundapur@gmail.com
Guest
 
Posts: n/a
#1: Jun 7 '06
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh

Ian Collins
Guest
 
Posts: n/a
#2: Jun 7 '06

re: How to get total heap size of a process


ganesh.kundapur@gmail.com wrote:[color=blue]
> Hi,
> Is there any way to get the total heap size allocated to a process
> in c on linux platform.
>[/color]
Ask in a Linux group? Heap size is an OS issue, not a C one.

--
Ian Collins.
CBFalconer
Guest
 
Posts: n/a
#3: Jun 7 '06

re: How to get total heap size of a process


"ganesh.kundapur@gmail.com" wrote:[color=blue]
>
> Is there any way to get the total heap size allocated to a process
> in c on linux platform.[/color]

Not portably, which is the only way that counts.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.


jacob navia
Guest
 
Posts: n/a
#4: Jun 7 '06

re: How to get total heap size of a process


ganesh.kundapur@gmail.com a écrit :[color=blue]
> Hi,
> Is there any way to get the total heap size allocated to a process
> in c on linux platform.
>
> Regards,
> Ganesh
>[/color]

This could give you an approximation:

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

int main(void)
{
unsigned int size=1;
unsigned int *p = &size;

while (p) {
p = malloc(size);
if (p) {
free(p);
size *= 2;
}
}
printf("Stopped at size=%u\n",size);
}
Keith Thompson
Guest
 
Posts: n/a
#5: Jun 7 '06

re: How to get total heap size of a process


CBFalconer <cbfalconer@yahoo.com> writes:[color=blue]
> "ganesh.kundapur@gmail.com" wrote:[color=green]
>> Is there any way to get the total heap size allocated to a process
>> in c on linux platform.[/color]
>
> Not portably, which is the only way that counts.[/color]

Well, it's the only way that counts *here*. Non-portable code is
sometimes perfectly appropriate; this just isn't usually the place to
discuss it.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Flash Gordon
Guest
 
Posts: n/a
#6: Jun 7 '06

re: How to get total heap size of a process


jacob navia wrote:[color=blue]
> ganesh.kundapur@gmail.com a écrit :[color=green]
>> Hi,
>> Is there any way to get the total heap size allocated to a process
>> in c on linux platform.
>>
>> Regards,
>> Ganesh
>>[/color]
>
> This could give you an approximation:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> unsigned int size=1;
> unsigned int *p = &size;
>
> while (p) {
> p = malloc(size);
> if (p) {
> free(p);
> size *= 2;
> }
> }
> printf("Stopped at size=%u\n",size);
> }[/color]

That does not tell you how much memory is allocated to the heap on any
system I've used recently since they increase the amount of memory
allocated to the heap as more requests are made. It *might* tell you the
largest power of two block you can allocate, or if the system is less
than optimal it might fail much earlier than that due to memory
fragmentation. There are lots of other problems with your suggestion
that are specific to Linux systems and so off topic here.

The correct advice of asking on a Linux group had already been given.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
jacob navia
Guest
 
Posts: n/a
#7: Jun 7 '06

re: How to get total heap size of a process


Flash Gordon wrote:[color=blue]
> jacob navia wrote:
>[color=green]
>> ganesh.kundapur@gmail.com a écrit :
>>[color=darkred]
>>> Hi,
>>> Is there any way to get the total heap size allocated to a process
>>> in c on linux platform.
>>>
>>> Regards,
>>> Ganesh
>>>[/color]
>>
>> This could give you an approximation:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main(void)
>> {
>> unsigned int size=1;
>> unsigned int *p = &size;
>>
>> while (p) {
>> p = malloc(size);
>> if (p) {
>> free(p);
>> size *= 2;
>> }
>> }
>> printf("Stopped at size=%u\n",size);
>> }[/color]
>
>
> That does not tell you how much memory is allocated to the heap on any
> system I've used recently since they increase the amount of memory
> allocated to the heap as more requests are made. It *might* tell you the
> largest power of two block you can allocate, or if the system is less
> than optimal it might fail much earlier than that due to memory
> fragmentation. There are lots of other problems with your suggestion
> that are specific to Linux systems and so off topic here.
>
> The correct advice of asking on a Linux group had already been given.[/color]

Yes, I misunderstood the question. Actually the OP wanted to know the
heap size of a running process, and I understood that he wanted to know
the maximum heap size for a process.

Sorry for the confusion.

jacob


Malcolm
Guest
 
Posts: n/a
#8: Jun 8 '06

re: How to get total heap size of a process




--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm

<ganesh.kundapur@gmail.com> wrote in message
news:1149676002.183198.18420@f6g2000cwb.googlegrou ps.com...[color=blue]
> Hi,
> Is there any way to get the total heap size allocated to a process
> in c on linux platform.
>
> Regards,
> Ganesh
>[/color]

int main(void)
{
/* puzzle. Why will this code break if we make a x a size_t ? */
unsigned long x = 0;

while(malloc(1))
x++;
printf("You have %lu bytes available\n", x);
return 0;
}


Andrew Poelstra
Guest
 
Posts: n/a
#9: Jun 8 '06

re: How to get total heap size of a process


<ganesh.kundapur@gmail.com> wrote in message
news:1149676002.183198.18420@f6g2000cwb.googlegrou ps.com...[color=blue]
> Hi,
> Is there any way to get the total heap size allocated to a process
> in c on linux platform.
>
> Regards,
> Ganesh
>[/color]

int main(void)
{
/* puzzle. Why will this code break if we make a x a size_t ? */
unsigned long x = 0;

while(malloc(1))
x++;
printf("You have %lu bytes available\n", x);
return 0;
}

Because printf can't handle a size_t?
Suman
Guest
 
Posts: n/a
#10: Jun 8 '06

re: How to get total heap size of a process



Andrew Poelstra wrote:[color=blue]
> <ganesh.kundapur@gmail.com> wrote in message
> news:1149676002.183198.18420@f6g2000cwb.googlegrou ps.com...[color=green]
> > Hi,
> > Is there any way to get the total heap size allocated to a process
> > in c on linux platform.
> >
> > Regards,
> > Ganesh
> >[/color]
>
> int main(void)
> {
> /* puzzle. Why will this code break if we make a x a size_t ? */[/color]

I am not sure either but is it because of this?

7.17 Common definitions <stddef.h>
....
Recommended practice
4 The types used for size_t and ptrdiff_t should not have an integer
conversion rank
greater than that of signed long int unless the implementation
supports objects
large enough to make this necessary.
[color=blue]
> unsigned long x = 0;
>
> while(malloc(1))
> x++;
> printf("You have %lu bytes available\n", x);
> return 0;
> }
>
> Because printf can't handle a size_t?[/color]
7.19.6.1
[#7] The length modifiers and their meanings are:
....
z Specifies that a following d, i, o, u, x, or X conversion specifier
applies to a
size_t or the corresponding signed integer type argument; or that a
following n conversion specifier applies to a pointer to a signed
integer type
corresponding to size_t argument.

Richard Bos
Guest
 
Posts: n/a
#11: Jun 8 '06

re: How to get total heap size of a process


jacob navia <jacob@jacob.remcomp.fr> wrote:
[color=blue]
> ganesh.kundapur@gmail.com a écrit :[color=green]
> > Is there any way to get the total heap size allocated to a process
> > in c on linux platform.[/color]
>
> This could give you an approximation:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> unsigned int size=1;
> unsigned int *p = &size;
>
> while (p) {
> p = malloc(size);
> if (p) {
> free(p);
> size *= 2;
> }
> }
> printf("Stopped at size=%u\n",size);[/color]
) }

Do this on a multi-user system, and it could tell you "Stopped at siThis
account has been terminated."

Richard

CBFalconer
Guest
 
Posts: n/a
#12: Jun 8 '06

re: How to get total heap size of a process


Andrew Poelstra wrote:[color=blue]
>[/color]
.... snip ...[color=blue]
>
> int main(void)
> {
> /* puzzle. Why will this code break if we make a x a size_t ? */
> unsigned long x = 0;
>
> while(malloc(1))
> x++;
> printf("You have %lu bytes available\n", x);
> return 0;
> }
>
> Because printf can't handle a size_t?[/color]

Exactly. You should cast x to (unsigned long). This will work on
C90 systems, provided only that an unsigned long can express a
size_t. For C99 you can use %z (I think).


--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.


Closed Thread