How to get total heap size of a process

  • Thread starter ganesh.kundapur
  • Start date
G

ganesh.kundapur

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

Regards,
Ganesh
 
I

Ian Collins

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

CBFalconer

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

jacob navia

(e-mail address removed) 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);
}
 
K

Keith Thompson

CBFalconer said:
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.
 
F

Flash Gordon

jacob said:
(e-mail address removed) a écrit :

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

jacob navia

Flash said:
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
 
M

Malcolm

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

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

Andrew Poelstra

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

Suman

Andrew said:
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.
 
C

CBFalconer

Andrew said:
.... 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.
 
R

Richard Bos

jacob navia said:
(e-mail address removed) a écrit :

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top