By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

How to get total heap size of a process

P: n/a
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh

Jun 7 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
ga*************@gmail.com wrote:
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Ask in a Linux group? Heap size is an OS issue, not a C one.

--
Ian Collins.
Jun 7 '06 #2

P: n/a
"ga*************@gmail.com" wrote:

Is there any way to get the total heap size allocated to a process
in c on linux platform.


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.
Jun 7 '06 #3

P: n/a
ga*************@gmail.com a écrit :
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh


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);
}
Jun 7 '06 #4

P: n/a
CBFalconer <cb********@yahoo.com> writes:
"ga*************@gmail.com" wrote:
Is there any way to get the total heap size allocated to a process
in c on linux platform.


Not portably, which is the only way that counts.


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) ks***@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.
Jun 7 '06 #5

P: n/a
jacob navia wrote:
ga*************@gmail.com a écrit :
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh


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);
}


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
Jun 7 '06 #6

P: n/a
Flash Gordon wrote:
jacob navia wrote:
ga*************@gmail.com a écrit :
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh


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);
}

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.


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
Jun 7 '06 #7

P: n/a


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

<ga*************@gmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com...
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh


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;
}
Jun 8 '06 #8

P: n/a
<ga*************@gmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com...
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh


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?
Jun 8 '06 #9

P: n/a

Andrew Poelstra wrote:
<ga*************@gmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com...
Hi,
Is there any way to get the total heap size allocated to a process
in c on linux platform.

Regards,
Ganesh

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


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

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.

Jun 8 '06 #10

P: n/a
jacob navia <ja***@jacob.remcomp.fr> wrote:
ga*************@gmail.com a écrit :
Is there any way to get the total heap size allocated to a process
in c on linux platform.


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

) }

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

Richard

Jun 8 '06 #11

P: n/a
Andrew Poelstra wrote:
.... snip ...
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?


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.
Jun 8 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.