Unix Programming - When a program crashes, is its memory reclaimed by the OS?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2006 > When a program crashes, is its memory reclaimed by the OS?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author When a program crashes, is its memory reclaimed by the OS?
D'artagnan

2006-05-23, 1:17 am

Dynamically allocated (malloc) memory is returned back to the OS when a
program terminates normally. But what if the program crashes? Is its
heap also freed? Thanks.

Erik Max Francis

2006-05-23, 1:17 am

D'artagnan wrote:

> Dynamically allocated (malloc) memory is returned back to the OS when a
> program terminates normally. But what if the program crashes? Is its
> heap also freed? Thanks.


Yes.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
To attempt to defend everything is to defend nothing.
-- Frederick the Great
Paul Pluzhnikov

2006-05-23, 1:17 am

"D'artagnan" <musketeers@gmail.com> writes:

> Dynamically allocated (malloc) memory is returned back to the OS when a
> program terminates normally. But what if the program crashes? Is its
> heap also freed?


The heap is not freed in the sense of free(3).

It simply disappears when the OS reclaims all resources allocated
to the process, and this reclamation happens regardless of whether
the process exited (normally of with an error), was killed by signal,
or exec()ed another program.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Russell Shaw

2006-05-23, 1:17 am

Paul Pluzhnikov wrote:
> "D'artagnan" <musketeers@gmail.com> writes:
>
>
> The heap is not freed in the sense of free(3).
>
> It simply disappears when the OS reclaims all resources allocated
> to the process, and this reclamation happens regardless of whether
> the process exited (normally of with an error), was killed by signal,
> or exec()ed another program.


It only works on systems with a hardware memory manager that keeps
track of the memory allocated to a process. On old DOS systems without
a MMU, a bad program will crash the system.
Frank Cusack

2006-05-23, 1:17 am

On Tue, 23 May 2006 15:02:37 +1000 Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
> Paul Pluzhnikov wrote:
>
> It only works on systems with a hardware memory manager that keeps
> track of the memory allocated to a process. On old DOS systems without
> a MMU, a bad program will crash the system.


Well, this is comp.*unix*.programmer. :-) Is there any Unix (or unix)
that doesn't reclaim memory? Certainly even today there are unix systems
that run on hardware without an MMU.

-frank
Hubble

2006-05-23, 1:17 am

>It only works on systems with a hardware memory manager that keeps
>track of the memory allocated to a process. On old DOS systems without
>a MMU, a bad program will crash the system.


It also works on Unix(-like) systems without an MMU, e.g. MINIX on 8086
(version1 and version2)

On most systems it works like this (even without MMU)

* A process has three segments
* TEXT for the program code (unchangable)
* DATA for initialized data
* BSS for uninitalized data

On start from an elf or a.out file, these segments are allocated as
needed.

The BSS segment can be growed and shrinked by the sbrk(2) system call.
Library functions like malloc use this call if they need more memory.
Some implementations never shrink the BSS (free keeps a list of
previously allocated memory, subsequent mallocs first consider if
memory in this list can be used, otherwise allocate a big chunk of
additional BSS space)

The OS keeps track of the segments. If a program exits or crashes, it
knows exactly the place and size of these segments and reclaims this
memory.

Hubble.

Maxim Yegorushkin

2006-05-23, 7:16 am


D'artagnan wrote:
> Dynamically allocated (malloc) memory is returned back to the OS when a
> program terminates normally. But what if the program crashes? Is its
> heap also freed? Thanks.


Adding to what others have said, shared memory has kernel persistence.
This means it has to be explicitly unlink()ed and the OS never does
that for you. The same applies for message queues and semaphores.

http://www.opengroup.org/onlinepubs...shm_unlink.html
http://www.opengroup.org/onlinepubs.../mq_unlink.html
http://www.opengroup.org/onlinepubs...sem_unlink.html

Alex Colvin

2006-05-23, 1:17 pm

>>>Dynamically allocated (malloc) memory is returned back to the OS when a
[vbcol=seagreen]
>It only works on systems with a hardware memory manager that keeps
>track of the memory allocated to a process. On old DOS systems without
>a MMU, a bad program will crash the system.


more like
It only works on an operating system, not on a program loader like DOS.

--
mac the naïf
anu

2006-05-24, 1:20 am

Hello Paul

How will we know that memory is free or not ? is there any procedure
to see the freed memory and allocated memory ?

bye

anagha


Paul Pluzhnikov wrote:
> "D'artagnan" <musketeers@gmail.com> writes:
>
>
> The heap is not freed in the sense of free(3).
>
> It simply disappears when the OS reclaims all resources allocated
> to the process, and this reclamation happens regardless of whether
> the process exited (normally of with an error), was killed by signal,
> or exec()ed another program.
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.


sandy

2006-05-24, 7:16 am


D'artagnan wrote:

> Dynamically allocated (malloc) memory is returned back to the OS when a
> program terminates normally. But what if the program crashes? Is its
> heap also freed? Thanks.


Google about " grabage collectors ". Its actually freed when the
garbage collector is executed.

Cheers,
Sandeepksinha.

Paul Pluzhnikov

2006-05-24, 7:16 pm

"sandy" <sandeepksinha@gmail.com> writes:

> D'artagnan wrote:
>
>
> Google about " grabage collectors ". Its actually freed when the
> garbage collector is executed.


That answer is *totally* incorrect.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
SM Ryan

2006-05-24, 7:16 pm

Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> wrote:
# "sandy" <sandeepksinha@gmail.com> writes:
#
# > D'artagnan wrote:
# >
# >> Dynamically allocated (malloc) memory is returned back to the OS when a
# >> program terminates normally. But what if the program crashes? Is its
# >> heap also freed? Thanks.
# >
# > Google about " grabage collectors ". Its actually freed when the
# > garbage collector is executed.
#
# That answer is *totally* incorrect.

The correct answer being "It depends." If the programs writes a core,
that's the VM image which is not collected until the file is deleted.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I ASSURE YOU WE'RE OPEN!
joe@invalid.address

2006-05-25, 7:18 am

SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> writes:

> Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> wrote:
> # "sandy" <sandeepksinha@gmail.com> writes:
> #
> # > D'artagnan wrote:
> # >
> # >> Dynamically allocated (malloc) memory is returned back to the OS when a
> # >> program terminates normally. But what if the program crashes? Is its
> # >> heap also freed? Thanks.
> # >
> # > Google about " grabage collectors ". Its actually freed when the
> # > garbage collector is executed.
> #
> # That answer is *totally* incorrect.
>
> The correct answer being "It depends." If the programs writes a
> core, that's the VM image which is not collected until the file is
> deleted.


The core file is a file, not a VM image. I think you're confusing
paging space and regular disk files. Any VM the program had allocated
is collected regardless of how the program ended, or whether or not a
disk file was created.

Joe
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com