Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Immediate free?

5 views
Skip to first unread message

R.van.Wijngaarden

unread,
Jul 8, 1997, 3:00:00 AM7/8/97
to

Question:

When executing my C-program on our Solaris5.4 workstation (SPARC20),
i noticed that my dynamically allocated memory (using malloc()) is only
really freed after my process/program dies.

Example:
...
p = (char *) malloc (1024 * 1024);
c = getchar();
free(p); --> not yet freed (acc. to Top/proctool)

c = getchar(); --> MEMORY REALLY FREED
--> AFTER PRESSING RETURN
.. process ended

Somebody (Rohan) mentioned that i'll need to use sbrk and roll
my own malloc/free.

e.g.
malloc(bufsize_in_bytes)
...
sbrk(-bufsize_in_bytes)


This works ok, but is very dangerous according to the manual
page of sbrk (since i already use malloc and free at other places)


Can anyone help me: i want a (non dangerous) immediate free
regardless if my process is still running or not. Do i need
some sort of flush?? or does the GCC malloc library
do the trick for me??. Maybe our version of the Solaris operating
system is not very good in handling dynamic memory?

Thanks for any help!

Rino.


ps: My C-program was compiled with gcc2.5.6

Dan Abarbanel

unread,
Jul 13, 1997, 3:00:00 AM7/13/97
to

R.van.Wijngaarden wrote:
>
> Question:
>
> When executing my C-program on our Solaris5.4 workstation (SPARC20),
> i noticed that my dynamically allocated memory (using malloc()) is > > > only really freed after my process/program dies.
>

> Somebody (Rohan) mentioned that i'll need to use sbrk and roll


> my own malloc/free.
>
> e.g.
> malloc(bufsize_in_bytes)
> ...
> sbrk(-bufsize_in_bytes)
>
> This works ok, but is very dangerous according to the manual
> page of sbrk (since i already use malloc and free at other places)
>
> Can anyone help me: i want a (non dangerous) immediate free
> regardless if my process is still running or not.

I'm pretty sure that all commercial/GPL/PD malloc packages toe the
line that says "Really free when program dies". As you've already
noted, it just makes life so much easier for the writers...
If you're desperate enough, you can write your OWN malloc/free, compile
them into a library, and use it instead of the standard malloc/free
(add -lMyAlloc to your link line if you call the lib libMyAlloc.[s|so])
Don't try this at home unless you're VERY sure of yourself ( I wouldn't
dare try it, myself).
Or, maybe someone has written something like this already and will
eventually answer you with a pointer to [his|her] work.

Regards,
Dan
--
======================================================================
Dan Abarbanel | "Warning: Dates in the calendar are
Madge Networks ESD, Israel | closer than they appear!"
Tel: +972 3 6457662 |
Fax: +972 3 6487146 |
e-mail: daba...@madge.com | Disclaimer: You call this an OPINION?

Andrew Gierth

unread,
Jul 13, 1997, 3:00:00 AM7/13/97
to

>>>>> "Dan" == Dan Abarbanel <daba...@madge.com> writes:

Dan> I'm pretty sure that all commercial/GPL/PD malloc packages toe the
Dan> line that says "Really free when program dies". As you've already
Dan> noted, it just makes life so much easier for the writers...

FreeBSD's malloc (at least in 2.2) can return memory back to the OS in
two different ways:

- optionally, freed pages are madvise'd to allow the kernel to discard
the previous data rather than having to fault the page in

- when reasonable to do so, the break level is reduced

The madvise part is system-dependent, but the rest of the code is less
so (it does need the ability to do an anonymous mmap, so it can keep
its page index separately from the arena proper).

(This malloc is sometimes referred to as "phkmalloc", after its author,
Poul-Henning Kamp).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>

0 new messages