473,549 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to find number of bytes freed

is there any way to find out number of bytes freed on a particular
free() call in C
Aug 8 '08
58 4864
sh******@gmail. com wrote:
is there any way to find out number of bytes freed on a particular
free() call in C
Not portably.

Usually you don't need to know; free frees what malloc malloced.

What is it that you'd do with the answer, were you to have it?

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Aug 8 '08 #11
On Aug 8, 7:52 pm, Chris Dollin <chris.dol...@h p.comwrote:
sh.vi...@gmail. com wrote:
is there any way to find out number of bytes freed on a particular
free() call in C

Not portably.

Usually you don't need to know; free frees what malloc malloced.

What is it that you'd do with the answer, were you to have it?

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

i am writing a simple application where i want to keep track of number
of bytes my process is using currently or peak memory used by my
process so far. i thought of using xyz_malloc() in stead of malloc
everywhere in my application and inside xyz_malloc add the count of
requested amount to a global variable. but when i have to free it i
need to reduce that count.

i can modify xyz_mallo() to solve this problem such that when asked to
allocate N number of bytes, allocate N+1 and return pointer to the 1st
onwards (not 0th) byte as a new memory to aplication and in first byte
store the number of bytes allocated. but it might cause serious
alignment problem

yea, probably my problem is not hard enough to require a memory
manager to be written. probably there must be some API to tell me
current memory usage.
if you know please let me know, but i would like to do some googling
my own before taking your valuable time on this.

--
thanks all
vipin
Aug 8 '08 #12
sh******@gmail. com wrote:
On Aug 8, 7:52 pm, Chris Dollin <chris.dol...@h p.comwrote:
>sh.vi...@gmail .com wrote:
>>is there any way to find out number of bytes freed on a particular
free() call in C
Not portably.

Usually you don't need to know; free frees what malloc malloced.

What is it that you'd do with the answer, were you to have it?

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


i am writing a simple application where i want to keep track of number
of bytes my process is using currently or peak memory used by my
process so far. i thought of using xyz_malloc() in stead of malloc
everywhere in my application and inside xyz_malloc add the count of
requested amount to a global variable. but when i have to free it i
need to reduce that count.
Your best bet is probably to use a system-specific tool to view
your program's memory consumption "from the outside," rather than
trying to get the information from inside the program. That's because
your program's total memory use certainly involves storage that your
code does not request from malloc() et al.: storage for code, for
static data, for automatic data ("the stack"), for I/O buffers and
suchlike things inside libraries ... Also, the usual reason to keep
track of things like peak memory usage is to monitor the health of
the system as a whole, not just of one program, so an external tool
is usually more useful because it can see many programs at once.
On Unixoid systems look for things like "top" or "prstat" or "ps"
or "pmap" or "vmstat."

That said, some implementations have system-specific ways to find
out things about your program's dynamically-allocated memory. See
if your system has a mallinfo() function and/or a <malloc.hheader ,
and check the system's documentation for how to use them and what
you can find out from them.
i can modify xyz_mallo() to solve this problem such that when asked to
allocate N number of bytes, allocate N+1 and return pointer to the 1st
onwards (not 0th) byte as a new memory to aplication and in first byte
store the number of bytes allocated. but it might cause serious
alignment problem
There are a couple of ways to cope with the alignment issue;
study the source code for actual allocators to get ideas. But you
still may be stuck with the problem of memory allocated by the
library -- your code may call xyz_malloc(), but fopen() most likely
gets its buffer memory from regular malloc() and thus "flies under
your radar."
yea, probably my problem is not hard enough to require a memory
manager to be written. probably there must be some API to tell me
current memory usage.
if you know please let me know, but i would like to do some googling
my own before taking your valuable time on this.
If you read this newsgroup for a while, you'll probably conclude
that many of us have *way* too much time on their hands ...

--
Er*********@sun .com
Aug 8 '08 #13
sh******@gmail. com wrote:
>
is there any way to find out number of bytes freed on a particular
free() call in C
No portable means. When you malloc (realloc, calloc) 'ed the item
you specified what you wanted. You can assume you got that much
back. However malloc can assign more than the requested memory
(for alignment maintenance reasons) in which case you will get back
more.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 8 '08 #14
Walter Roberson said:
In article <ic************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>>sh******@gmai l.com said:
>>is there any way to find out number of bytes freed on a particular
free() call in C
>>When you allocate this storage, you know how much storage you allocated.
>>Don't Forget.
>>Then, when you come to free it, you know, right?

The number of bytes of the allocation request is not necessarily
the same as the number of bytes freed.
True, but not particularly important in the grand scheme of things as far
as the abstract machine is concerned. When you request an allocation, it
either works or it doesn't. If it doesn't, tough. If it does, the most
number of bytes you can rely on having is the number you asked for - any
overhead is the implementation' s business, not yours. So if you think of
your program in abstract terms (as I tend to do), the allocation
*effectively* only gives you the N bytes you asked for (even if there is
some behind-the-scenes overhead going on), and therefore when you free
that block you *effectively* free N bytes. Yes, there may be overhead, but
no, from an abstract machine perspective it turns out not to matter to
your program.

If you move away from the abstract machine, and want to know how much
*actual* memory is being booted from pillar to post and back, then you're
into the realms of implementation-specific extensions, which are
considered atopical in clc.

In other words, I think we both know what we both mean, and we're both
right in our own sweet way. :-)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 8 '08 #15
In article <79************ *************** *******@b2g2000 prf.googlegroup s.com>,
<sh******@gmail .comwrote:
>i am writing a simple application where i want to keep track of number
of bytes my process is using currently or peak memory used by my
process so far.
On most systems, memory can only be requested from the system in
"pages" (of some system-specific size); anything left over in the
"page" that you haven't allocated yet would not be counted by your
program, but it is still "peak memory used by your process".

Counting allocated / deallocated memory also does not take into
account that due to fragmentation, there is almost always memory
allocated to your process but which is not allocated to any
particular user object (some might argue it is allocated to the
memory allocator itself, but details like that are system-specific.)

And as I mentioned in one of my previous responses, there are overhead
bytes needed by the memory allocator to track what is available or free;
those are part of your process but if you only keep a count of your
net requests then you would not be including those.
--
"Tired minds don't plan well. Sleep first, plan later."
-- Walter Reisch
Aug 8 '08 #16

<sh******@gmail .comwrote in message
news:79******** *************** ***********@b2g 2000prf.googleg roups.com...
On Aug 8, 7:52 pm, Chris Dollin <chris.dol...@h p.comwrote:
>sh.vi...@gmail .com wrote:
is there any way to find out number of bytes freed on a particular
free() call in C

Not portably.

Usually you don't need to know; free frees what malloc malloced.

What is it that you'd do with the answer, were you to have it?
i am writing a simple application where i want to keep track of number
of bytes my process is using currently or peak memory used by my
process so far.
I wrote a memory allocator once where it's free() function /required/ the
number of bytes to free, as the allocator did not store this. I don't
remember that this was ever a problem: usually you knew or could calculate
the N needed.

C's free() of course does not need it (although it means extra overhead to
store it somewhere). But that needn't stop you creating a function such as
free_n(P,N) and using that everywhere.
i can modify xyz_mallo() to solve this problem such that when asked to
allocate N number of bytes, allocate N+1 and return pointer to the 1st
onwards (not 0th) byte as a new memory to aplication and in first byte
store the number of bytes allocated. but it might cause serious
alignment problem
You will need more than 1 byte. Use 8 or 16 instead.

But, your OS might have some utility to tell your the memory usage of your
application.

--
Bartc

Aug 8 '08 #17
On 8 Aug 2008 at 15:02, sh******@gmail. com wrote:
i am writing a simple application where i want to keep track of number
of bytes my process is using currently or peak memory used by my
process so far.

yea, probably my problem is not hard enough to require a memory
manager to be written. probably there must be some API to tell me
current memory usage.
There is, of course, but the people here will swear blind it doesn't
exist with all the fervor of a madman claiming the sky is not blue.

For example, with gcc you can #include <malloc.hand use mallinfo().
You can also probably get information from your kernel about your
process's memory usage: for example, on Linux you could investigate
/proc/<pid>/status. How useful the information you'll get is is another
question though...

Aug 8 '08 #18
Richard Heathfield <rj*@see.sig.in validwrites:
Walter Roberson said:
>In article <ic************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>>>sh******@gma il.com said:
>>>is there any way to find out number of bytes freed on a particular
free() call in C
>>>When you allocate this storage, you know how much storage you allocated.
>>>Don't Forget.
>>>Then, when you come to free it, you know, right?

The number of bytes of the allocation request is not necessarily
the same as the number of bytes freed.

True, but not particularly important in the grand scheme of things as far
as the abstract machine is concerned.
Yes. But in the real world someone wanting to know the amount used is,
err, probably actually interested in such things.

Wouldn't you think?
Aug 8 '08 #19
In article <sl************ *******@nospam. invalid>,
Antoninus Twink <no****@nospam. invalidwrote:
>On 8 Aug 2008 at 15:02, sh******@gmail. com wrote:
>There is, of course, but the people here will swear blind it doesn't
exist with all the fervor of a madman claiming the sky is not blue.
There are a lot of times that the sky is not blue. Like when it is
night out. Or when there are heavy clouds. Severe weather such as
close approximations of torados can turn the sky *very* strange
colors -- as can some kinds of severe snow storms.

"the sky is blue" is not a portable assumption.
--
"Why does he stagger his mind with the mathematics of the sky?
Once the question mark has arisen in the human brain the answer must
be found, if it takes a hundred years. A thousand years."
-- Walter Reisch
Aug 8 '08 #20

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

Similar topics

2
2064
by: Vince | last post by:
I have a very specific problem to solve but I cannot find a data structure for it. I don't know if I am posting on the good newsgroup but I cannot find a software.design group. I would like to declare a smart structure initialized via a XML file. The goal of this structure is to store data from a smart card. My XML file describes the file...
3
2221
by: subirs | last post by:
Hi, I am using mtrace to check for memory leaks in my code. The code is divided into many fucntion which are placed in different directories. While using mtrace the following output is given. Now the problem is to find where in the code and in which function memory leaks occurs as the trace does not give line number or name of the ...
22
3996
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to receive 5 bytes buffer , I call the BeginReceive and then wait on AsyncWaitHandle.WaitOne() but it is signald imidiatly , and the next call to...
64
8305
by: Robert Seacord | last post by:
The C standard doesn't say anything about what happens when you call realloc with a size argument of 0. Both glibc and openbsd appear to return a valid pointer to a zero-sized object.. e.g. the return of a malloc(0). Does anyone know of a runtime where realloc() free'ed the object and then returned NULL? If so, it would make the following...
4
2376
by: joyce | last post by:
hi How would I go abouts finding out how many bytes a string has. ( I want to do this so that I can set the datatype on the database approriately.) Thanks Joyce
5
3816
by: Prabhakar Rao | last post by:
For ex if memory allocated like below p = malloc(100) . . . . . free(p) how the free routine know, how much memory to be freed?
0
7532
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...
0
7462
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...
0
7730
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. ...
0
7975
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7492
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...
0
7823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6059
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...
0
3510
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...
0
3491
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.