Desperately looking for a command

Desperately looking for a command

Post by Mark Mill » Thu, 29 Jun 1995 04:00:00



Could any guru out there tell me if there exists a "C" function that I can
call within a UNIX application that will pass back a value telling me how
much 'current memory' has been allocated and 'in-use'???   We're trying
to eliminate memory leaks in our application and if we could periodically
'poke' the OS and check that value, we could easily see if we're running
into leaks, etc....

Since I usually don't read these news groups that often, I would be extremely
grateful if you have an answer to reply directly to my account (and the rest of
the news group if you like) and I would hope to help return the favor in
another issue/caveat that you may need assistance/opinions.  I have
checked out the "Steven's Unix Book (the bible)" and the Marice Baush
"Design of the UNIX operating system" and could not readily find such
a system call (or a series of system calls), where in a similar DOS
application of ours (where we're using VOS (Voice Operating System))
we have a call like:

        printf("The amount of free memory currently = %ld\n", freemem());

.

thank you kindly,
theron b kousek

 
 
 

Desperately looking for a command

Post by Christian A. Ladema » Tue, 04 Jul 1995 04:00:00



>Could any guru out there tell me if there exists a "C" function that I can
>call within a UNIX application that will pass back a value telling me how
>much 'current memory' has been allocated and 'in-use'???   We're trying
>to eliminate memory leaks in our application and if we could periodically
>'poke' the OS and check that value, we could easily see if we're running
>into leaks, etc....
>Since I usually don't read these news groups that often, I would be extremely
>grateful if you have an answer to reply directly to my account (and the rest of
>the news group if you like) and I would hope to help return the favor in
>another issue/caveat that you may need assistance/opinions.  I have
>checked out the "Steven's Unix Book (the bible)" and the Marice Baush
>"Design of the UNIX operating system" and could not readily find such
>a system call (or a series of system calls), where in a similar DOS
>application of ours (where we're using VOS (Voice Operating System))
>we have a call like:
>    printf("The amount of free memory currently = %ld\n", freemem());
>.
>thank you kindly,
>theron b kousek

Try sbrk(0) to get the amount of allocated space. As the maximum amount of
virtual memory per process is kernel-configuable in most UNIX'es, freemem()
is not as usefull for UNIX as it may be for DOS.

There are also freely availabe memory-de* libraries with accounting
malloc(), free(), etc. routines.

cu, Christian.

--
----------------------------------------------------------------------

--------------------- speaking from, not for: ------------------------
    ZLS Software GmbH                         Tel.:  (+49) 6195 900500
     D-65779 Kelkheim                         Fax:   (+49) 6195 900600
----------------------------------------------------------------------

 
 
 

Desperately looking for a command

Post by Ke Ji » Thu, 06 Jul 1995 04:00:00




>>Could any guru out there tell me if there exists a "C" function that I can
>>call within a UNIX application that will pass back a value telling me how
>>much 'current memory' has been allocated and 'in-use'???   We're trying
>>to eliminate memory leaks in our application and if we could periodically
>>'poke' the OS and check that value, we could easily see if we're running
>>into leaks, etc....

>Try sbrk(0) to get the amount of allocated space.

        sbrk(0) will not give amount of allocated space, it returns the
        address of upper boundary of heap in process's virtual space.
        If you want the total size of heap space, try this:

                int heapsize()
                {
                        extern int end;

                        return sbrk(0) - (int)&end;
                }

        It should be cleared that the heap size is usually large than amount of
        allocated space. Because, 1. Most systems will not lower heap upper

        boundary when free() been called(you can lower it by a brk() call if you
        like). It just keep the freed blocks for later malloc(). 2. memory
        management module itself need some dynamic space also (i.e. to keep
        free block table, etc), 3. Not all blocks/fragments in heap will/can be
        used by malloc call(for alignment and efficient reason).

Quote:>There are also freely availabe memory-de* libraries with accounting
>malloc(), free(), etc. routines.

        They are useful in finding the memory leak but have some limition.
        First, some library (e.g. stdio) will call malloc(). You can replace
        malloc/free in your code but not libraries. Second, You need to set
        the check point in your code in order to catch the leak right after
        it happen. Thus, it require recompilations of your source code.
        Commerical de*s get scope of pointers in a compiled(with -g)
        program by reading its symbol table and trace the process with
        ptrace(). It will report the memory aligment and memory leak(e.g.
        out of scope) problems right on the points.