473,385 Members | 1,372 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,385 software developers and data experts.

Memory usage

hello all

i want to know in my program the amount of memory allocated by
malloc() calls
so i "overloaded" the malloc functions to increment a counter of
memory used (no call to free is done).
this counter is incremented each time a malloc is done by the number
of requested machine words.
The problem is that when i do a "ps aux" the amount of memory
allocated for my program is quite different from the amount i computed
manually.
So my question is : what does really do a malloc call?
for example if i do this

ptr = malloc(sizeof(char) * 65);

am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
the memory allocated will be of 68 bytes?

any help would be appreciated
Sami Evangelista
Nov 14 '05 #1
4 1455
Evangelista Sami wrote:
hello all

i want to know in my program the amount of memory allocated by
malloc() calls
so i "overloaded" the malloc functions to increment a counter of
memory used (no call to free is done).
this counter is incremented each time a malloc is done by the number
of requested machine words.
The problem is that when i do a "ps aux" the amount of memory
allocated for my program is quite different from the amount i computed
manually.
So my question is : what does really do a malloc call?
for example if i do this

ptr = malloc(sizeof(char) * 65);

am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
the memory allocated will be of 68 bytes?


The exact amount depends on the implementation.

<OT>
Many implementations of malloc() alocate slightly more for internal
administration. Often this extra information is placed before the
address you receive from malloc(). This extra information can be
the size of the block; things that might help in the design of free().

Furthermore, malloc() itself, for efficiency, might request memory
from a lower layer in larger chunks. So with the 'ps' command you
might observe bumps. Since you seem to be on UNIX, having a look at
the manual page of sbrk() (and related calls) might be interesting.

Things like this causes, 'ps' to shows a considereably higher memory
usage than you expected.
</OT>

Case

Nov 14 '05 #2

"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
hello all

i want to know in my program the amount of memory allocated by
malloc() calls
so i "overloaded" the malloc functions to increment a counter of
memory used (no call to free is done).
this counter is incremented each time a malloc is done by the number
of requested machine words.
The problem is that when i do a "ps aux" the amount of memory
allocated for my program is quite different from the amount i computed
manually.
So my question is : what does really do a malloc call?
for example if i do this

ptr = malloc(sizeof(char) * 65);

am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
the memory allocated will be of 68 bytes?

any help would be appreciated
Sami Evangelista


This is completely implementation specific. I know of one system that at
start of day 'grabs' a chunk of memory (e.g. 1kB) then as you malloc, it
will use bits of this chunk. If you require more, it gets an exponentially
bigger chuk from the OS and gives you some of that, so you will be seeing
your program use a lot more memory than you expect.
Allan
Nov 14 '05 #3
"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
So my question is : what does really do a malloc call?
for example if i do this

ptr = malloc(sizeof(char) * 65);

am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
the memory allocated will be of 68 bytes?

any help would be appreciated


It depends upon the way malloc() is actually implemented by your platform's
library. All that is guaranteed is that the number of bytes you requested
will be allocated (maybe more, maybe much more, but never less) and that the
memory pointer returned will be aligned for any type of use on your
platform. It is quite "normal" for malloc to allocate memory in blocks (e.g.
multiples of 256 bytes) or increase the size you asked for by the overhead
of the malloc management header (the pointer to the next allocated block and
this block's size etc.) which it could store immediately before your "user"
memory.

To work out how your platform allocates memory, you could attempt to display
the void * returned by three consecutive malloc(2); on may platforms these
will not be 2 bytes different.
Nov 14 '05 #4

"Evangelista Sami" <ev******@cnam.fr> wrote in
news:5f**************************@posting.google.c om...
am i sure that (assuming a machine word = 4 bytes and 1 char = 1 byte)
the memory allocated will be of 68 bytes?


It doesn't have to. malloc() is only required (and expected to) allocate
_at_least_ the amount you ask it.
The reason is that some (all really) OSes will give you chunks of memory
rather than the exact amount asked for. This makes memory management much
easier.
In short. You can only be sure about the minimum memory your program will
use.
In large programs the difference in usage wil probably be barely noticeable
though.

--
Carlos Martin
SoKrA-BTS
Be paranoid: Encrypt your mail.
Nov 14 '05 #5

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

Similar topics

8
by: rbt | last post by:
Would a Python process consume more memory on a PC with lots of memory? For example, say I have the same Python script running on two WinXP computers that both have Python 2.4.0. One computer has...
5
by: Justice | last post by:
Currently I'm doing some experimenting with the XMLHTTP object in Javascript. Now, the XMLHttp object is asynchronous (at least in this case), and the following code causes a significant memory...
2
by: tomvr | last post by:
Hello I have noticed some 'weird' memory usage in a vb.net windows app The situation is as follows I have an app (heavy on images) with 2 forms (actually there are more forms and on starting...
6
by: Tom | last post by:
We have a VERY simple .NET C# Form Application, that has about a 23MB Memory Footprint. It starts a window runs a process and does a regular expression. I have done a GC.Collect to make sure that,...
2
by: Jarvis | last post by:
I've made a testing program to test the memory usage of some Data Forms. I create a MDI parent form with one single MDI child form, which is a Data Form generated by .NET Data Form Wizard. To...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
20
by: Philip Carnstam | last post by:
How come .Net applications use so much memory? Every application I compile uses at least 10 MB of memory, even the ones consisting of only a form and nothing else. If I minimize them though the...
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
1
by: yzghan | last post by:
Hi all, I feel that my python script is leaking memory. And this is a test I have: log.write(" " + "test() ... memory usage: " + " ".join(repr(i/(1024*1024)) for i in getMemInfo()) + "\n") m...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.