R
Ramasubbu Ramasubramanian XR (AS/EAB)
What is memory leakage, could any one explain with sample code
Ramasubbu said:What is memory leakage, could any one explain with sample code
void *X;What is memory leakage, could any one explain with sample code
Mark said:When your program slowly consumes system memory.
int main(void)
{
char *pMembloc;
// Never free().
for (;
{
pMemblock = malloc(1024);
}
return 0;
}
Ramasubbu said:What is memory leakage, could any one explain with sample code
Slightly off-topic, when a program terminates, all the memory it had
allocated is returned to the system, right? (Yeah, I know, system
specific...) Or are there many systems that don't do that?
Ben Pfaff said:This is "normally" the case, but it is not guaranteed by the
standard.
True, this is a QoI issue.
A conservative assumption is that we can replace "normal"
by "highly normal"?
I guess Ben P. too remember, that Dan
Pop once gave an example of such an OS. There was also
another c.l.c. regular that didn't trust Windows NT, to do such
a cleanup properly in all cases.
Nils Weller said:Some people keep saying that not free()'ing everything before program
termination is potentially dangerous, but I have never seen anyone
provide a *concrete* example of an implementation that actually
requires this to avoid memory leaks.
It appears that there are no such implementations, and even if you
could
come up with one or two exotic, historical and barely used examples,
then they were highly unlikely to be a viable target for software
development in C today. And even if they do exist and are a viable
target to some, then the fact that the implementation cannot even
clean
up all memory implies that it is a highly specialized and limited
platform where free() vs. no free() is just one among many other
problems that need to be addressed (or in other words: if you're
writing
for such a system, then free() is the least of your worries, and
religiously calling free() all the time will not magically make your
code work well on it.)
The fact is that neither the C standard, nor the known C
implementations
(unless someone can finally provide a counter example), provide any
basis for having to call free() before program termination.
I can't recall seeing Dan Pop mention an example implementation, but I
do recall him giving another good reason as to why the free() argument
is bogus: If the system cannot clean up the heap, what will happen to
all other memory of your program - the memory whose deallocation is
even
more (and I say ``more'' because most common free() implementations do
not actually return anything to the operating system; In most cases
they
just save it for subsequent malloc() requests, and the hardware page
size makes this technically impossible for small memory chunks anyway)
outside of your control, typically stack, code and data segments?
Nils Weller said:[...]"Ben Pfaff" <[email protected]> wrote in message
Some people keep saying that not free()'ing everything before program
termination is potentially dangerous, but I have never seen anyone
provide a *concrete* example of an implementation that actually
requires this to avoid memory leaks.
As I said, Dan Pop gave _one_ such an example, I would put it
in the exotic & historical category, can't even remember what it
was called anymore.![]()
I more or less agree with this. However, if writing a non-stop server,
I care a lot about *not* leaking any memory. OTOH, short-lived
programs running on a stable kernel with VM support, I don't see the
problem with "memory leaking" at all.
Nils Weller said:Nils Weller said:[...]
As I said, Dan Pop gave _one_ such an example, I would put it
in the exotic & historical category, can't even remember what it
was called anymore.![]()
I recall that Dan Pop mentioned an example of a system where the
operating system is not capable of freeing the memory, but on that
implementation (Amiga, IIRC), the C runtime takes care of the issue,
so it is NOT an example of an implementation where you have to
call free() in order to prevent memory leakage.
Ahh.. yes Amiga DOS was the one! You are right, there was cleanup
code in the runtime library...
Dan's chief point, was that if a system isn't able to reclaim unfree'ed
memory on program termination, then the same would be likely for
the free'ed memory.
What is memory leakage, could any one explain with sample code
Slightly off-topic, when a program terminates, all the memory it had
allocated is returned to the system, right? (Yeah, I know, system
specific...) Or are there many systems that don't do that?
Tor said:True, this is a QoI issue.
A conservative assumption is that we can replace "normal"
by "highly normal"? I guess Ben P. too remember, that Dan
Pop once gave an example of such an OS. There was also
another c.l.c. regular that didn't trust Windows NT, to do such
a cleanup properly in all cases.
Ben Pfaff said:This is "normally" the case, but it is not guaranteed by the
standard.
Slightly off-topic, when a program terminates, all the memory it had
allocated is returned to the system, right? (Yeah, I know, system
specific...) Or are there many systems that don't do that?
Ramasubbu Ramasubramanian XR (AS/EAB) wrote on 23/03/05 :
When you allocate some memory block and never free it.
On 24h per day
running machines, such a behaviour is lethal.
Even assuming that a compiler's C run time library walked its active
allocation list and free'd everything after main() returned or exit()
was called, sometimes programs crash, preventing any clean up in the
run time library from executing.
As for those who ask for concrete examples, there were early versions
of MS-DOS, 2.x for sure and I think 3.x, the could lose it if a
program terminated without deallocating memory it allocated while
running. You would end up with a prompt on the screen something like
"Can't load command.com, system halted".
It has been argued that since C doesn't require unfreed memory to be
reclaimed on program termination, that is a reason to make sure
that all allocated memory is freed. However C doesn't require that any
resources used by the C program be reclaimed (except that files be
closed
on normal termination); static, automatic variables, program code and
even
freed memory need not be reclaimed either. So this is not itself a
reason
to free memory. It is possible that real-world implementations don't
reclaim unfreed memory but that's rare to non-existant these days.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.