home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / question / 10455 < prev    next >
Encoding:
Text File  |  1992-08-27  |  2.8 KB  |  89 lines

  1. Xref: sparky comp.unix.questions:10455 comp.lang.c:12901
  2. Path: sparky!uunet!cs.utexas.edu!news
  3. From: zhu@cs.utexas.edu (Kai Zhu)
  4. Newsgroups: comp.unix.questions,comp.lang.c
  5. Subject: Free memory, memory usage
  6. Followup-To: poster
  7. Date: 27 Aug 1992 22:23:10 -0500
  8. Organization: CS Dept, University of Texas at Austin
  9. Lines: 75
  10. Message-ID: <l9r70uINNl2s@panda.cs.utexas.edu>
  11. NNTP-Posting-Host: panda.cs.utexas.edu
  12. Keywords: free(), ps -u
  13.  
  14. I run the following program to see the effect of free() function
  15. and system call "ps -u" for measuring memory usage by the process:
  16.  
  17. ========================
  18. #include <stdio.h>
  19. #include <malloc.h>
  20.  
  21. main()
  22. {
  23.    printf("***** before function.\n");
  24.    system("ps -ux | grep 'a.out'");
  25.    sub();
  26.    printf("***** after function.\n");
  27.    system("ps -ux | grep 'a.out'");
  28.  
  29. }
  30.  
  31. sub()
  32. {
  33.    int num[500000], i;
  34.    char *ptr[2];
  35.  
  36.    for (i = 0; i < 2; i++) {
  37.       printf("i = %d\n", i);
  38.       if ((ptr[i] = (char *) malloc(1000000)) == NULL) {
  39.      printf("Cannot get memory\n");
  40.      exit(1);
  41.       }
  42.       system("ps -ux | grep 'a.out'");
  43.    }
  44.    for (i = 0; i < 2; i++) {
  45.       printf("i = %d\n", i);
  46.       free(ptr[i]);
  47.       system("ps -ux | grep 'a.out'");
  48.    }
  49. }
  50. ======================
  51.  
  52. The outputs are (irrelevant outputs are deleted):
  53.  
  54. =====================
  55. USER       PID %CPU %MEM   SZ  RSS TT STAT START  TIME COMMAND /* I add this line */
  56. ***** before function.
  57. zhu      21565  7.7  1.3   24  188 p8 S    22:02   0:00 a.out
  58. i = 0
  59. zhu      21565  3.9  1.4 2956  204 p8 S    22:02   0:00 a.out
  60. i = 1
  61. zhu      21565  2.7  1.4 3936  204 p8 S    22:02   0:00 a.out
  62. i = 0
  63. zhu      21565  2.1  1.4 3936  208 p8 S    22:02   0:00 a.out /* memory usage increases*/
  64. i = 1
  65. zhu      21565  1.7  1.4 3936  204 p8 S    22:02   0:00 a.out
  66. ***** after function.
  67. zhu      21565  0.0  1.4 3936  208 p8 S    22:02   0:00 a.out /* memory increases */
  68. ======================
  69.  
  70. My questions are:
  71. (1) Is "ps -u" the command to check the memory usage by a process 
  72.     during its execution ?
  73. (2) If the anwser to (1) is yes, why the memory used by the process increases
  74.     after ptr[0] was freed ?  The man page of ps says the field RSS is 
  75.     real memory usage, and the field SZ is the combined size of the data
  76.     and stack segments. What is the difference between RSS and SZ ?
  77. (3) After calling function sub(), the memory usage is larger than that
  78.     before calling sub() and SZ stays the same.  Why the memory used 
  79.     by the array num[] in sub() was not released after calling sub() ?
  80. (4) I tried out any testing program.  After freeing a string, the content
  81.     of the string can still be printed out.  This means free(ptr) does not set
  82.     ptr (or any pointer) to NULL. Does this imply the memory is not freed ? 
  83.  
  84. Any explanations will be greatly appreciated.
  85. Apology if the posting is too long.
  86.  
  87. -Kai Zhu
  88. zhu@cs.utexas.edu
  89.