473,473 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to get total heap size of a process

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
11 17610
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
"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
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
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
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
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


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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dima | last post by:
How can I know size of avaible memory in heap? For example : .... .... // size = N cout << "Size of Heap = " << SizeOfHeap() << endl; int* i = new int; // size = N - sizeof(int) cout << "Size...
1
by: nrhayyal | last post by:
hi all, i am working on C++ on AIX machine. i am running one of my module which is built using 10+ user built libraries. i am getting St9bad_alloc exception. i came to know that this exception...
4
by: rd | last post by:
Hi i'm uncertain about foll. things. Thanks in advance for any help to get me out of this.. >>Each process, with their own stack, will they have their own heap or is it global one(common for...
9
by: shine | last post by:
what is the difference between a heap and a stack?
10
by: Pep | last post by:
I have a problem I need to solve which looks like memory exhaustion. Is there a function that I can use in c++ to determine the size of the heap before and after a allocation? The C++ code is...
7
by: Raman | last post by:
Hi All, Could any one tell me, how can I determine/Change size of heap on per- process basis on Unix based systems. Thanks. Regards
2
by: sammiesue | last post by:
Hi, I have form with 2 autosummed textboxes ("total" and "casinototal"). I would like to have a grand total textbox ("grandtotal") get its value from summing "total" and "casinototal", but it...
3
by: emer.kurbegovic | last post by:
how can i get heap memory of a windows process with C#? For example, I would like to see the value of the heap memory allocated for my "java.exe" process. thanks in advance.
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.