473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Memory used by the program



Hi,

I am developing some code in C which first I compile on my linux
machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all. Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:

- what will be the stack size available for the program
- what will be the size of the code
- what will be the size of the allocated data (global variables, etc)
- if it is also possible to estimate the size of the heap used

Thanks in advance
H
Sep 22 '08 #1
8 2116
On 22 Sep, 09:51, Horacius ReX <horacius....@gmail.comwrote:
I am developing some code in C which first I compile on my linux
machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all. Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:

- what will be the stack size available for the program
- what will be the size of the code
- what will be the size of the allocated data (global variables, etc)
- if it is also possible to estimate the size of the heap used
I'm afraid this is all very platform specific. And on "special"
hardware probably even more restricted.

On embedded platforms you may be able to specify the stack size
at compile or load time.

You should be able to deduce the size of your executable and the
size of staically allocated data by examining the image (somehow).

Heap you should be able to calculate at run time by tracking
malloc() and free().

Try a ng group specialising in embedded software.
--
Nick Keighley

Many astrologers think that this concentration on [the sun-sign
column] has
done untold damage to serious astrology.
The Independent
Sep 22 '08 #2
On Sep 22, 11:56*am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 22 Sep, 09:51, Horacius ReX <horacius....@gmail.comwrote:
I am developing some code in C which first I compile on my linux
machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all. Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:
- what will be the stack size available for the program
- what will be the size of the code
- what will be the size of the allocated data (global variables, etc)
- if it is also possible to estimate the size of the heap used

I'm afraid this is all very platform specific. And on "special"
hardware probably even more restricted.

On embedded platforms you may be able to specify the stack size
at compile or load time.

You should be able to deduce the size of your executable and the
size of staically allocated data by examining the image (somehow).

Heap you should be able to calculate at run time by tracking
malloc() and free().

Try a ng group specialising in embedded software.

--
Nick Keighley

Many astrologers think that this concentration on [the sun-sign
column] has
done untold damage to serious astrology.
* * * * * * * * * * * * The Independent
Ok, what do you mean with "image" ?
Sep 22 '08 #3
>I am developing some code in C which first I compile on my linux
>machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all.
Just about everything you are asking for is system-specific.

Do the host system and the target system at least have the same
CPU architecture? Code size can be radically different for the
same program on, say, the PDP-8 vs. the x86 architecture.
>Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:

- what will be the stack size available for the program
The stack size *available* depends on your hardware and how you
lay out the address. If space is really, really tight you may
have to move the split between, say, stack and data for every
recompilation to make it fit.

You probably want the stack size *required*, which is harder to figure.

Each function call requires a certain amount of stack, generally
the size of all auto variables for that function plus some overhead
for linkage, unless you're using variable-length arrays or alloca().
For [3456]86 architecture, I'll guess about 32 bytes of linkage
overhead. Look at generated code for a more accurate answer. You
may also need some initial stack overhead for whatever calls main(),
like command-line arguments if these are used.

The requirement of the whole program is the worst-case requirement
of all functions, which depends on what functions call what other
functions. If the program is recursive, this might be near-infinite,
in which case your limited-memory system should likely be using a
different non-recursive algorithm.

For example:
main() uses 100 bytes and calls A, B, and C.
A uses 200 bytes and calls C.
B uses 800 bytes and doesn't call anything.
C uses 300 bytes and doesn't call anything.
D uses 15000 bytes (but nobody calls it).

The worst case paths are:

main+A+C = 100+200+300 = 600.
main+B = 100+800 = 900.
main+C = 100+300 = 400.

The worst case here is main calling B, 900 bytes.
>- what will be the size of the code
- what will be the size of the allocated data (global variables, etc)
If the executable produced has headers like those produced by standard
Linux tools (and the version you run *on Linux* will almost certainly
have them), the size(1) command will give you the size of code,
(initialized) data, and uninitialized data. (This does not, however,
include the size of any shared libraries (on Linux) or the BIOS or
OS on the target system.)

Other tools such as objdump may give you finer detail on portions of
the object code.
>- if it is also possible to estimate the size of the heap used
This is a runtime issue. If possible, put monitoring in the program
that runs on Linux to track the maximum amount of simultaneously
allocated memory from malloc(). Otherwise, estimate it by hand.
The result may depend on input to the program. It might even depend
on *timing* of input to the program (e.g. if this thing is acting
as a router and it buffers packets if it can't handle them fast
enough, up to a limit.)

malloc() has overhead. On a 32-bit machine, rounding the requested
amount up to a multiple of 4 and add 4 is typical of a couple of
malloc() implementations.

Sep 22 '08 #4
On 22 Sep, 16:10, Horacius ReX <horacius....@gmail.comwrote:
On Sep 22, 11:56*am,Nick Keighley<nick_keighley_nos...@hotmail.com>
wrote:
On 22 Sep, 09:51, Horacius ReX <horacius....@gmail.comwrote:
I am developing some code in C which first I compile on my linux
machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all. Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:
- what will be the stack size available for the program
- what will be the size of the code
- what will be the size of the allocated data (global variables, etc)
- if it is also possible to estimate the size of the heap used
in general you can't find heap size without running the program.
You could look at the heap size on the linux system
and use that as an indication of the special hardware
heap size (they won't, in general, match as the size of
allocated objects will be different- and malloc overheads).

<snip>
You should be able to deduce the size of your executable and the
size of staically allocated data by examining the image (somehow).
<snip>
--
Nick Keighley
Many astrologers think that this concentration on [the sun-sign
column] has
done untold damage to serious astrology.
* * * * * * * * * * * * The Independent
you shouldn't normally quote sigs (the bit after "-- ")

Ok, what do you mean with "image"
executable. (note must stop reading The Standard for fun)
--
Nick Keighley

Quantum Boggum Sort:
Q1. use a source of quantum noise (eg. radioactive decay) to
randomly permutate an array.
Q2. if the array is not ordered, destroy the universe (*)
Q3. if you reached this step your universe has sorted the array
in O(n) time.
(*) [100] this is left as an exercise
Sep 23 '08 #5
Horacius ReX wrote:
>
Hi,

I am developing some code in C which first I compile on my linux
machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all. Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:

- what will be the stack size available for the program
This is can be worked out from a map file. Most if not all embedded
compilers can generate one.
- what will be the size of the code
Map file.
- what will be the size of the allocated data (global variables, etc)
Map file.
- if it is also possible to estimate the size of the heap used
If you are resource constrained, go for a static design then you won't
have to worry.

--
Ian Collins.
Sep 23 '08 #6
On Sep 23, 12:37*am, gordonb.ca...@burditt.org (Gordon Burditt) wrote:
I am developing some code in C which first I compile on my linux
machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all.

Just about everything you are asking for is system-specific.

Do the host system and the target system at least have the same
CPU architecture? *Code size can be radically different for the
same program on, say, the PDP-8 vs. the x86 architecture.
Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:
- what will be the stack size available for the program

The stack size *available* depends on your hardware and how you
lay out the address. *If space is really, really tight you may
have to move the split between, say, stack and data for every
recompilation to make it fit.

You probably want the stack size *required*, which is harder to figure.

Each function call requires a certain amount of stack, generally
the size of all auto variables for that function plus some overhead
for linkage, unless you're using variable-length arrays or alloca().
For [3456]86 architecture, I'll guess about 32 bytes of linkage
overhead. *Look at generated code for a more accurate answer. *You
may also need some initial stack overhead for whatever calls main(),
like command-line arguments if these are used.

The requirement of the whole program is the worst-case requirement
of all functions, which depends on what functions call what other
functions. *If the program is recursive, this might be near-infinite,
in which case your limited-memory system should likely be using a
different non-recursive algorithm.

For example:
main() uses 100 bytes and calls A, B, and C.
A uses 200 bytes and calls C.
B uses 800 bytes and doesn't call anything.
C uses 300 bytes and doesn't call anything.
D uses 15000 bytes (but nobody calls it).

The worst case paths are:

main+A+C = 100+200+300 = 600.
main+B = 100+800 = 900.
main+C = 100+300 = 400.

The worst case here is main calling B, 900 bytes.
- what will be the size of the code
- what will be the size of the allocated data (global variables, etc)

If the executable produced has headers like those produced by standard
Linux tools (and the version you run *on Linux* will almost certainly
have them), the size(1) command will give you the size of code,
(initialized) data, and uninitialized data. *(This does not, however,
include the size of any shared libraries (on Linux) or the BIOS or
OS on the target system.)

Other tools such as objdump may give you finer detail on portions of
the object code.
- if it is also possible to estimate the size of the heap used

This is a runtime issue. *If possible, put monitoring in the program
that runs on Linux to track the maximum amount of simultaneously
allocated memory from malloc(). *Otherwise, estimate it by hand.
The result may depend on input to the program. *It might even depend
on *timing* of input to the program (e.g. if this thing is acting
as a router and it buffers packets if it can't handle them fast
enough, up to a limit.)

malloc() has overhead. *On a 32-bit machine, rounding the requested
amount up to a multiple of 4 and add 4 is typical of a couple of
malloc() implementations.
thanks for a so good and complete answer !
Sep 23 '08 #7
On Sep 23, 10:26*am, Ian Collins <ian-n...@hotmail.comwrote:
Horacius ReX wrote:
Hi,
I am developing some code in C which first I compile on my linux
machine and afterwards
I test on another special hardware which has almost no debug
capabilities at all. Usually I get a lot of errors in the latter,
because I have memory size limitations. So, I wonder what are the best
practices to know for a given program, once it is compiled and
before its execution:
- what will be the stack size available for the program

This is can be worked out from amapfile. *Most if not all embedded
compilers can generate one.
- what will be the size of the code

Mapfile.
- what will be the size of the allocated data (global variables, etc)

Mapfile.
- if it is also possible to estimate the size of the heap used

If you are resource constrained, go for a static design then you won't
have to worry.

--
Ian Collins.
where can one get extensive documentation about the info generated in
a map file ?

i achieved to get it, but no google, nothing about it

thanks

Sep 25 '08 #8
Horacius ReX wrote:
On Sep 23, 10:26 am, Ian Collins <ian-n...@hotmail.comwrote:
>>
>>- what will be the size of the allocated data (global variables, etc)
Mapfile.
>>- if it is also possible to estimate the size of the heap used
If you are resource constrained, go for a static design then you won't
have to worry.
*Please* don't quote signatures.
>
where can one get extensive documentation about the info generated in
a map file ?
From your compiler's documentation.

--
Ian Collins.
Sep 26 '08 #9

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

Similar topics

14
by: J. Campbell | last post by:
what happens to allocated memory when a program terminates before the memory is released. For example: int main(){ int* array; int a_size = 1000; array = new int; for(int i = 0; i < a_size;...
9
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...
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
4
by: Hermann Maier | last post by:
hi, i need to find out the memory usage of a specific function that i use in my program. this function does some recursive calculations and i want my program to display the amount of memory the...
13
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as...
2
by: Asfar | last post by:
Hi, I have a VS2005 windows program written in c#. In this program I have an array list which stores many DataTable's. When I first run the pogram the arraylist is loaded with datatables. At...
66
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...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.