473,695 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory required for a c program

Hi All,

I need to know the memory required by a c program. Is there any
tool/utility which can give me the memory usage in terms of DATA
segment, TEXT segment, BSS segment etc.

I am working on linux platform and my target is ARM processor. But i
guess it should not matter. Actually i need to know both RAM & ROM
usage.

I searched the messages posted on this group as well as on the embedded
group but could not find anything meaningful. Would appreciate the
help.

Regards
Hemal

Sep 25 '06 #1
9 4529

Hemal wrote:
Hi All,

I need to know the memory required by a c program. Is there any
tool/utility which can give me the memory usage in terms of DATA
segment, TEXT segment, BSS segment etc.

I am working on linux platform and my target is ARM processor. But i
guess it should not matter. Actually i need to know both RAM & ROM
usage.

I searched the messages posted on this group as well as on the embedded
group but could not find anything meaningful. Would appreciate the
help.

Regards
Hemal
look for "size" in the compilers bin directory

-Lasse

Sep 25 '06 #2
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
Hemal <im*****@gmail. comwrote:
>I need to know the memory required by a c program. Is there any
tool/utility which can give me the memory usage in terms of DATA
segment, TEXT segment, BSS segment etc.
You might be able to get some of those, but it isn't difficult to
prove that there cannot be any general tool which can reliably
examine source and calculate the amount of stack and heap required
by arbitrary programs.

(For example, you can write a simple iterative search for odd
"perfect numbers", and when you find one, allocate that number
of bytes. Despite extensive searches, there are no known odd
perfect numbers, but there is also no proof that they cannot
exist, so in order for a program to be able to analyze the source
and predict its memory usage, the examining program would first have to
solve the "odd perfect number" problem...)

>I am working on linux platform and my target is ARM processor. But i
guess it should not matter.
Yes, it matters a lot, as a number of operating systems do not
use DATA or TEXT or BSS segments at all.
>Actually i need to know both RAM & ROM
usage.

I searched the messages posted on this group as well as on the embedded
group but could not find anything meaningful. Would appreciate the
help.
It is not surprising you didn't find it in comp.lang.c, as such
tool would be platform dependant and we strongly avoid discussion of
platform dependancies here. You should check a linux programming
group.
[Off topic]
If your platform offers the elfdump or dwarfdump tools, you might
be able to dig out some information of interest.

Also, your platform's linker might support a "registry file",
possibly named so_locations .

--
All is vanity. -- Ecclesiastes
Sep 25 '06 #3
Thanks. "size" gave me the information & i believe it is ROM size.

Although below question may not belong to this group but in
continuation to above question

Is there any difference between RAM & ROM size for same code & if yes
why? My understanding is ROM size is large as it contains additional
ReadOnly TEXT section.

Thanks
-Hemal

la******@ieee.o rg wrote:
Hemal wrote:
Hi All,

I need to know the memory required by a c program. Is there any
tool/utility which can give me the memory usage in terms of DATA
segment, TEXT segment, BSS segment etc.

I am working on linux platform and my target is ARM processor. But i
guess it should not matter. Actually i need to know both RAM & ROM
usage.

I searched the messages posted on this group as well as on the embedded
group but could not find anything meaningful. Would appreciate the
help.

Regards
Hemal

look for "size" in the compilers bin directory

-Lasse
Sep 25 '06 #4
Thanks for your help but i couldn't make out from what you mean by "odd
perfect numbers".

-Hemal
Walter Roberson wrote:
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
Hemal <im*****@gmail. comwrote:
I need to know the memory required by a c program. Is there any
tool/utility which can give me the memory usage in terms of DATA
segment, TEXT segment, BSS segment etc.

You might be able to get some of those, but it isn't difficult to
prove that there cannot be any general tool which can reliably
examine source and calculate the amount of stack and heap required
by arbitrary programs.

(For example, you can write a simple iterative search for odd
"perfect numbers", and when you find one, allocate that number
of bytes. Despite extensive searches, there are no known odd
perfect numbers, but there is also no proof that they cannot
exist, so in order for a program to be able to analyze the source
and predict its memory usage, the examining program would first have to
solve the "odd perfect number" problem...)

I am working on linux platform and my target is ARM processor. But i
guess it should not matter.

Yes, it matters a lot, as a number of operating systems do not
use DATA or TEXT or BSS segments at all.
Actually i need to know both RAM & ROM
usage.

I searched the messages posted on this group as well as on the embedded
group but could not find anything meaningful. Would appreciate the
help.

It is not surprising you didn't find it in comp.lang.c, as such
tool would be platform dependant and we strongly avoid discussion of
platform dependancies here. You should check a linux programming
group.
[Off topic]
If your platform offers the elfdump or dwarfdump tools, you might
be able to dig out some information of interest.

Also, your platform's linker might support a "registry file",
possibly named so_locations .

--
All is vanity. -- Ecclesiastes
Sep 25 '06 #5
In article <11************ **********@m7g2 000cwm.googlegr oups.com>,
Hemal <im*****@gmail. comwrote:
>Thanks for your help but i couldn't make out from what you mean by "odd
perfect numbers".
http://en.wikipedia.org/wiki/Perfect_number

The exact meaning wasn't relevant to the discussion; the
point was that it was there was no known theoretical way to
solve the problem.

Recapping, the point was that there *cannot* be a program
which reliably predicts run-time memory usage of arbitrary programs,
even if full source (or executable) is available.

The proof that such programs *cannot* exist was a simple proof by
contradiction: assume that such a program, P, exists; now run P on
a program Q. Q is a program that finds a certain number by trial
(test each number in turn) when there is no analytic way
to find those kinds of numbers. Add a twist to the program Q such
that once it has found the number, it allocates an amount of memory
equal to the number it has found. Now, in order for P run on Q to
predict how much memory Q will allocate, P would have to be able to
analyze Q and solve what Q would find: only once P knows analytically
the answer to Q can P know how much memory Q would eventually allocate.
However, in order for P to know analytically what Q will do, P
would have to find an analytic way to calculate something that
our premises said does not *have* an analytic solution. Since P
cannot do the impossible, we conclude that program P does not exist.
There are, though -some- programs that can be analyzed for memory use;
there just isn't any way to analyze -every- program.

But you didn't ask for a tool to analyze the memory usage of a
-particular- program, you {implicitly} asked for a tool to analyze
the memory usage of whatever programs you happen to construct. You
were looking for a general analysis tool... and those CANNOT exist
(at least and give the correct answer :-) )
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Sep 25 '06 #6
Hemal wrote:
>
I need to know the memory required by a c program. Is there any
tool/utility which can give me the memory usage in terms of DATA
segment, TEXT segment, BSS segment etc.

I am working on linux platform and my target is ARM processor.
But i guess it should not matter. Actually i need to know both
RAM & ROM usage.
This is system specific, and thus off-topic on c.l.c.
comp.unix.progr ammer might be able to help. In general you should
look at your system documentation for a way to make the link phase
emit a loadmap.

--
Some informative links:
<news:news.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Sep 25 '06 #7

Walter Roberson wrote:
In article <11************ **********@m7g2 000cwm.googlegr oups.com>,
Hemal <im*****@gmail. comwrote:
Thanks for your help but i couldn't make out from what you mean by "odd
perfect numbers".

Recapping, the point was that there *cannot* be a program
which reliably predicts run-time memory usage of arbitrary programs,
even if full source (or executable) is available.

The proof that such programs *cannot* exist was a simple proof by
contradiction: assume that such a program, P, exists; now run P on
a program Q. Q is a program that finds a certain number by trial
(test each number in turn) when there is no analytic way
to find those kinds of numbers. Add a twist to the program Q such
that once it has found the number, it allocates an amount of memory
equal to the number it has found. Now, in order for P run on Q to
predict how much memory Q will allocate, P would have to be able to
analyze Q and solve what Q would find: only once P knows analytically
the answer to Q can P know how much memory Q would eventually allocate.
However, in order for P to know analytically what Q will do, P
would have to find an analytic way to calculate something that
our premises said does not *have* an analytic solution. Since P
cannot do the impossible, we conclude that program P does not exist.
This is completely off-topic nitpicking, but the odd perfect number
approach is not a real proof. This is a common error in computability
theory: people assume that highly difficult tasks are uncomputable,
which can hardly be further from the truth. There's a famous trick
question, "Let f(x)=1 if God exists, 0 if not. Is f(x) computable?"
The answer is of course "yes" since the constantly 1 function and the
constantly 0 function are both computable and f is one of these.

A correct proof that a general memory-predictor is impossible is more
subtle than meets the eye, because a looping program can, during its
infinite loop, chew up more and more memory, so the trick (which you're
essentially trying to use) of allocating a big block upon halting,
doesn't really work. No matter how big a block we choose, for all we
know the program might chew up precisely that much memory and yet still
not halt, so our memory-predictor sheds no light on the halting problem
that way. Probably the easiest way to prove the program in question is
impossible, is Rice's theorem. Of course it would be nice if someone
can come up with an easier proof, since Rice's theorem is rather
overkill here...

Sep 26 '06 #8
In article <11************ **********@e3g2 000cwe.googlegr oups.com>,
Hemal <im*****@gmail. comwrote:
>Although below question may not belong to this group but in
continuation to above question
>Is there any difference between RAM & ROM size for same code & if yes
why? My understanding is ROM size is large as it contains additional
ReadOnly TEXT section.
That'd be implementation dependant...

ROM can be large; ROM can be small; RAM can be large, RAM can be small.
Some systems have none of one; some have none of the other.

You might have a program that needs a lot of writable memory (RAM) to
do its calculations, even though the code might be small and the
number of constants to look up might be small. For example, it
might calculate the exact value of 3 multiplied by itself a billion
times. Just a lot of repeated calculations.

You might have a program that can do the bulk of its work from static
lookup tables, without needing much working memory. For such a program,
the static tables could be put in ROM and a small RAM used.

Pure ROM systems (no RAM other than CPU registers) can be found in
a few embedded systems such as toasters.

Pure RAM systems, with no ROM (or equivilent) at all, are more
difficult, but can be constructed from "static RAM", "non-volatile RAM",
and similar memory types that are writable but do not lose information
when the power is removed. Failing that, there's always hardware-level
bootstrapping :-)
--
Programming is what happens while you're busy making other plans.
Sep 26 '06 #9

Snis Pilbor wrote:
Walter Roberson wrote:
In article <11************ **********@m7g2 000cwm.googlegr oups.com>,
Hemal <im*****@gmail. comwrote:
>Thanks for your help but i couldn't make out from what you mean by "odd
>perfect numbers".
Recapping, the point was that there *cannot* be a program
which reliably predicts run-time memory usage of arbitrary programs,
even if full source (or executable) is available.

The proof that such programs *cannot* exist was a simple proof by
contradiction: assume that such a program, P, exists; now run P on
a program Q. Q is a program that finds a certain number by trial
(test each number in turn) when there is no analytic way
to find those kinds of numbers. Add a twist to the program Q such
that once it has found the number, it allocates an amount of memory
equal to the number it has found. Now, in order for P run on Q to
predict how much memory Q will allocate, P would have to be able to
analyze Q and solve what Q would find: only once P knows analytically
the answer to Q can P know how much memory Q would eventually allocate.
However, in order for P to know analytically what Q will do, P
would have to find an analytic way to calculate something that
our premises said does not *have* an analytic solution. Since P
cannot do the impossible, we conclude that program P does not exist.

This is completely off-topic nitpicking, but the odd perfect number
approach is not a real proof. This is a common error in computability
theory: people assume that highly difficult tasks are uncomputable,
which can hardly be further from the truth. There's a famous trick
question, "Let f(x)=1 if God exists, 0 if not. Is f(x) computable?"
The answer is of course "yes" since the constantly 1 function and the
constantly 0 function are both computable and f is one of these.

A correct proof that a general memory-predictor is impossible is more
subtle than meets the eye, because a looping program can, during its
infinite loop, chew up more and more memory, so the trick (which you're
essentially trying to use) of allocating a big block upon halting,
doesn't really work. No matter how big a block we choose, for all we
know the program might chew up precisely that much memory and yet still
not halt, so our memory-predictor sheds no light on the halting problem
that way. Probably the easiest way to prove the program in question is
impossible, is Rice's theorem. Of course it would be nice if someone
can come up with an easier proof, since Rice's theorem is rather
overkill here...
Ah, upon further consideration, I realized Rice's theorem doesn't even
apply, since this is a question which depends upon the model of
computation, not just the functions themselves.

But I managed to think up a proof.

Theorem: Assuming we're working on an ideal computer with infinite
memory, no program P exists which can predict how much memory an
arbitrary program Q will require.

Proof: Suppose such a program P exists. We'll now solve the halting
problem. Suppose n is an integer, so we need to give an algorithm to
tell whether the program with machine code n, when run on input n, will
halt. Proceed as follows. Write a program Q which simulates the
program with machine code n, with input n, but set it up so the
simulation always invokes twice as much memory as the simulated program
asks for. Also ensure that the simulator itself uses an even number of
bytes for any overhead it needs (even if this means allocating
additional dummy memory). Throughout the simulation, keep track of the
maximum memory required by the simulator at any point so far. If the
simulated program ever halts, then Q will free all memory, and allocate
a block of odd size, and so big that it is bigger than the biggest
amount of memory required hitherto. Now the program with machine code
n, given input n, will halt if and only if Q will require an odd number
of bytes, and we can determine this by plugging Q into P. So we've
solved the halting problem, absurd, therefore no such P exists.

Sep 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3843
by: benevilent | last post by:
Hey, I'm trying to debug the memory allocation in an embedded use of the Python interpreter. The longer I leave my program running the more memory it consumes. The total number of objects in the system is not increasing (many are being allocated and deallocated). Using mtrace I have established that the only memory which is not being
16
2883
by: Justin Lazanowski | last post by:
Cross posting this question on the recommendation of an I have a .NET application that I am developing in C# I am loading information in from a dataset, and then pushing the dataset to a grid, or other controls depending on the particular form. This application is setup with one MDI parent calling MDI children with the exception of one Modal form (the report viewer). When I run the application and run one of the screens that pulls...
9
2344
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When the OS is asking for it is usually too late, tons of swapping and slow performance.
3
2708
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of dynamic memory required for my datastructures ( used sizeof() to get the size of the structures ). But mallinfo() function show that approximately double that amount of memory is being allocated. I am working with gcc 3.2.2 on a redhat 9 machine. I...
8
2924
by: vikram | last post by:
i have series of questions 1.How a c program is loaded in memory i mean the whats is the structure that the code segment?? data segment?? 2.When you say const int *p; where is p stored in the memory?? what happens internal so that its a read only. 3. when declared volatile int *p where exactly in the memory it is stored.
72
3596
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
7
1853
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of code that basically acts as a server and that will be running for weeks or months and probably even longer, memory management is a topic that is quite crucial.
5
1959
by: Robin Tucker | last post by:
I've noticed that my program executable remains in the process list if at any stage my program has shown a FolderBrowserDialog. I am calling .Dipose on the dialog after use. This does not happen when I go through the code paths that do not create and display this dialog. Is the folder browser hanging onto something that is holding the rest of my program active on the task list? Any help would be appreciated :) Thanks.
66
3614
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
0
8619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8555
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8824
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7651
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5831
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4336
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2258
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.