Subject: Re: calloc() and free() ps doesn'r report memory being freed, why?
To: John Maier <JohnAM@datastorm.com>
From: Elmar Kolkman <kolkmae@la1.apd.dec.com>
List: netbsd-help
Date: 07/31/1996 11:02:25
Hi,

> I have been hacking on ftpd so I can add an alternate ftp password   
> filelist.  This allows me to give explicit access to individule without   
> compromising system security.  Also I can allow others to administer the   
> auxiliary ftp passwords with out having to give them su privlages.
> 
> I added a feature to allow the sub-administrator the ability to limit   
> logins to a particular number of logins (3 in my case).  To do this I   
> added a function that is called after a correct password has been   
> entered.  I noticed, however, that the ftpd proccess memory usage went   
> through the cealing!  On a 64Meg machine, which I have never seen in   
> swap, I now get up to 14Megs into swap.
> 
> I am using free() like a good boy, by ps -maux shows VSZ=2264 and   
> RSS=2052.  On a anonymous FTP login (which by-passes this function) The   
> memory usage, as reported by ps, is VSZ=196 and RSS=520.
> 
> So what am I doing wrong?  Do I need to worry about this?  Is there   
> something I can do to get the OS to compact the memory for a proccess?
> 
> Thanks
> jam

Well, I have followed this discussion with interest. But, I don't think it
this case has anything to do with NetBSD.
See the code:

> 
> 
> /* code fragment */
> int ReduceLogin(char *szUser)
> {
>  unsigned int iCnt=0;
>  int iState=0;
>  passwdfile *password[NUMBER_OF_ENTRIES];

So, it's a array of NUMBER_OF_ENTRIES elements, namely pointers to passwdfile
types.

> 
>  *password = calloc(NUMBER_OF_ENTRIES, sizeof(passwdfile));

Hey, you're allocating the first entry in the table with another (through
calloc) table of NUMBER_OF_ENTRIES passwdfile types.
>  
>  /* manipulate data */
>  ~
>  ~
>  ~
>  ~
>  /* end data manipulation */
> 
>  free(*password); /* added out of desparation */

You're freeing your calloced table. This seems right.

>  free(password);  /* free the gobs (1.5 Meg) of memory allocated */

Hey. RING THE BELL. You're freeing your array from your function header. This
cannot be true....

>  return iState;
> }

There are two problems/solutions.
What I would do:

	/* code fragment */
	int ReduceLogin(char *szUser)
	{
	   unsigned int iCnt=0;
	   int iState=0;
	   passwdfile *password;

	   password = calloc(NUMBER_OF_ENTRIES, sizeof(passwdfile));

	   /* manipulate data */
	   ~
	   ~
	   ~
	   ~
	   /* end data manipulation */
	
	   free(password);  /* free the gobs (1.5 Meg) of memory allocated */

	   return iState;
	}

When I look at the comment you added in the code, I think that is your
solution.

BTW.: this doesn't solve the related problems which are discussed in this
      thread about process memory.


						Elmar

-- 
+--------------------------------+---------------------------------------------+
| If brevity is the soul of wit  |  kolkmae@apd.dec.com / elmar@usn.nl         |
| Length must be the body of it  |  Elmar Kolkman     In weekend : 071-5891734 |
|                    John O'Mill |  Tel. 055-5432104. After 20.00: 055-5785293 |
+--------------------------------+---------------------------------------------+