[PLUG] Getting the amount of free memory in C

Tyler F. Creelan creelan at engr.orst.edu
Fri Nov 8 00:43:31 UTC 2002


> the best approaches are: a) program in a way that's provably non-leaky.
> b) use a language where memory leaks can't happen.

Languages not designed for garbage collectors usually work well with
conservative GCs, like Boehm-Weiser. Even though it's too expensive for
the GC to explicitly tag and deallocate every pointer (for unions
especially) such GCs ensure inaccessible memory remains bounded, something
that rarely happens manually.

IIRC, Boehm-Weiser is the GC used in Mono's .NET runtime. I heard it's
also used in Mozilla, so actual results may vary. ;)

Tyler


On Thu, 7 Nov 2002, Felix Lee wrote:

> Russell Senior <seniorr at aracnet.com>:
> > Not sure that will work.  According to the man page, "Not all fields
> > are meaningful under Linux.  Right now (Linux 2.4) only the fields
>
> sheesh.  linux sucks :)
>
> ok, sbrk(0) will give you a pointer to the current end of
> the data segment in your process, which is probably where
> malloc gets new memory from.  this isn't the size of your
> process, but you can get meaningful deltas.
>     void* old_edata = sbrk (0);
>     /* do some stuff */
>     void* new_edata = sbrk (0);
>     ptrdiff_t diff = new_edata - old_edata;
>
> but if the goal is to detect memory leaks, this isn't all
> that useful, because malloc/free doesn't necessarily shrink
> the process size ever.
>
> detecting memory leaks is a hard problem.  the best
> approaches are: a) program in a way that's provably
> non-leaky.  b) use a language where memory leaks can't
> happen.
> --
>
> _______________________________________________
> PLUG mailing list
> PLUG at lists.pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug
>






More information about the PLUG mailing list