home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.questions:10455 comp.lang.c:12901
- Path: sparky!uunet!cs.utexas.edu!news
- From: zhu@cs.utexas.edu (Kai Zhu)
- Newsgroups: comp.unix.questions,comp.lang.c
- Subject: Free memory, memory usage
- Followup-To: poster
- Date: 27 Aug 1992 22:23:10 -0500
- Organization: CS Dept, University of Texas at Austin
- Lines: 75
- Message-ID: <l9r70uINNl2s@panda.cs.utexas.edu>
- NNTP-Posting-Host: panda.cs.utexas.edu
- Keywords: free(), ps -u
-
- I run the following program to see the effect of free() function
- and system call "ps -u" for measuring memory usage by the process:
-
- ========================
- #include <stdio.h>
- #include <malloc.h>
-
- main()
- {
- printf("***** before function.\n");
- system("ps -ux | grep 'a.out'");
- sub();
- printf("***** after function.\n");
- system("ps -ux | grep 'a.out'");
-
- }
-
- sub()
- {
- int num[500000], i;
- char *ptr[2];
-
- for (i = 0; i < 2; i++) {
- printf("i = %d\n", i);
- if ((ptr[i] = (char *) malloc(1000000)) == NULL) {
- printf("Cannot get memory\n");
- exit(1);
- }
- system("ps -ux | grep 'a.out'");
- }
- for (i = 0; i < 2; i++) {
- printf("i = %d\n", i);
- free(ptr[i]);
- system("ps -ux | grep 'a.out'");
- }
- }
- ======================
-
- The outputs are (irrelevant outputs are deleted):
-
- =====================
- USER PID %CPU %MEM SZ RSS TT STAT START TIME COMMAND /* I add this line */
- ***** before function.
- zhu 21565 7.7 1.3 24 188 p8 S 22:02 0:00 a.out
- i = 0
- zhu 21565 3.9 1.4 2956 204 p8 S 22:02 0:00 a.out
- i = 1
- zhu 21565 2.7 1.4 3936 204 p8 S 22:02 0:00 a.out
- i = 0
- zhu 21565 2.1 1.4 3936 208 p8 S 22:02 0:00 a.out /* memory usage increases*/
- i = 1
- zhu 21565 1.7 1.4 3936 204 p8 S 22:02 0:00 a.out
- ***** after function.
- zhu 21565 0.0 1.4 3936 208 p8 S 22:02 0:00 a.out /* memory increases */
- ======================
-
- My questions are:
- (1) Is "ps -u" the command to check the memory usage by a process
- during its execution ?
- (2) If the anwser to (1) is yes, why the memory used by the process increases
- after ptr[0] was freed ? The man page of ps says the field RSS is
- real memory usage, and the field SZ is the combined size of the data
- and stack segments. What is the difference between RSS and SZ ?
- (3) After calling function sub(), the memory usage is larger than that
- before calling sub() and SZ stays the same. Why the memory used
- by the array num[] in sub() was not released after calling sub() ?
- (4) I tried out any testing program. After freeing a string, the content
- of the string can still be printed out. This means free(ptr) does not set
- ptr (or any pointer) to NULL. Does this imply the memory is not freed ?
-
- Any explanations will be greatly appreciated.
- Apology if the posting is too long.
-
- -Kai Zhu
- zhu@cs.utexas.edu
-