├── COPYING ├── README └── memwatch.c /COPYING: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) <2014> 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | MemWatch 2 | 3 | What is MemWatch? 4 | MemWatch is a small utility that prints the actual memory(RAM) used by an Application. It let's you give memory usage in percentage at every instruction executed. 5 | 6 | Why MemWatch? 7 | If you are facing memory leak issue in your code and wants to debug the issue by executing each line and function, you are at right place. MemWatch is for you. It takes your application name and starts printing the Memory usage used by your application. Now in your code you place the debug point before the function and step over the function and watched the memory usage, if it has increased, that's the evil function in your code that eats the memory. Similar there are many technique to detect the leak, MemWatch will print the actual physical RAM usage used by your Application/Program. 8 | 9 | 10 | 11 | 12 | Usage: 13 | gcc -Wall -O3 -o MemWatch memwatch.c 14 | 15 | ./MemWatch YourAppName 16 | 17 | for ex. 18 | /usr/bin/MemWatch firefox 19 | Total Ram Application Percentage 20 | 1026708kB 149684kB 14.579% 21 | 1026708kB 149684kB 14.579% 22 | 1026708kB 149684kB 14.579% 23 | 1026708kB 149684kB 14.579% 24 | 25 | 26 | Suggestion/Improvements/Bugs are welcome. :-) 27 | 28 | 29 | /skanzariya 30 | -------------------------------------------------------------------------------- /memwatch.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * 4 | * Usage ./AppMemory Appname 5 | */ 6 | 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | int main(int ac, char *av[]); 21 | int CheckUsage(const char*); 22 | 23 | 24 | /*Function open the statm file for the Application and monitor the usage constantly*/ 25 | int CheckUsage(const char *filename) 26 | { 27 | 28 | struct sysinfo info; 29 | 30 | if( sysinfo(&info)!= 0 ) 31 | perror("Sysinfo\n"); 32 | 33 | unsigned long TotalRam = 0; 34 | TotalRam = (info.totalram * info.mem_unit) / 1024; 35 | 36 | printf("%10s %12s %13s","Total Ram","Application","Percentage\n"); 37 | //printf("Check file %s\n", filename); 38 | 39 | 40 | while (1) { 41 | 42 | FILE *FP; 43 | FP = fopen(filename,"r"); 44 | 45 | if(FP == NULL){ 46 | fprintf(stderr,"Application close or File '%s' does not exists\n", filename); 47 | return 1; 48 | } 49 | 50 | unsigned long size=0; 51 | unsigned long resident=0; 52 | unsigned long share=0; 53 | unsigned long text=0; 54 | unsigned long lib=0; 55 | unsigned long data=0; 56 | unsigned long dt=0; 57 | 58 | fscanf(FP,"%lu %lu %lu %lu %lu %lu %lu",&size, &resident, &share, &text, &lib, &data, &dt); 59 | 60 | unsigned long TotalUsage = (resident * getpagesize()); 61 | 62 | float Percentage = 0.0f; 63 | Percentage = (float)(((float)TotalUsage/1024)/(float)TotalRam)*100; 64 | 65 | 66 | printf("%10lukB %10lukB %10.3f%%\n", TotalRam, TotalUsage/1024, Percentage); 67 | 68 | fclose(FP); 69 | 70 | sleep(1); 71 | } 72 | 73 | return 1; 74 | } 75 | 76 | int main(int ac, char *av[]) 77 | { 78 | const char *AppName = av[1]; 79 | if(AppName==NULL){ 80 | fprintf(stderr,"Usage %s Appname\n", (char *)basename(av[0])); 81 | exit(EXIT_FAILURE); 82 | } 83 | 84 | DIR *ProcDir = NULL; 85 | 86 | DIR *ProcDirDir = NULL; 87 | struct dirent *pDir = NULL; 88 | struct dirent *pDirDir = NULL; 89 | 90 | ProcDir = opendir("/proc"); 91 | if (ProcDir == NULL){ 92 | fprintf(stderr,"Can't Open /proc directory \n"); 93 | return 1; 94 | } 95 | 96 | 97 | char buf[128] = ""; 98 | while ((pDir = readdir(ProcDir)) != NULL){ 99 | 100 | /*skip . and ..*/ 101 | if(strlen(pDir->d_name)<=2)continue; 102 | 103 | /*skip file and symlink directory*/ 104 | if(((int)pDir->d_type == 8) || ((int)pDir->d_type == 10)) continue; 105 | //printf("Directory is %s %d\n", pDir->d_name, (int)pDir->d_type); 106 | 107 | sprintf(buf,"/proc/%s", pDir->d_name); 108 | 109 | /*Open Directory inside the proc directory*/ 110 | ProcDirDir = opendir(buf); 111 | while ((pDirDir = readdir(ProcDirDir))!=NULL){ 112 | 113 | if (strlen(pDirDir->d_name)<=2 )continue; 114 | 115 | if((pDirDir->d_type == 4) || (pDirDir->d_type == 10))continue; 116 | 117 | //printf("Inside Directory %s \n", pDirDir->d_name) 118 | 119 | if(strcmp(pDirDir->d_name,"cmdline") == 0){ 120 | 121 | char cmdline[128] = ""; 122 | sprintf(cmdline,"/proc/%s/%s",pDir->d_name, pDirDir->d_name); 123 | //printf("We found the cmdline file for %s of %s %s\n", pDirDir->d_name, pDir->d_name, cmdline); 124 | 125 | FILE *fp; 126 | fp = fopen(cmdline,"r"); 127 | if(fp==NULL){ 128 | fprintf(stderr,"Can't open file %s \n", cmdline); 129 | closedir(ProcDirDir); 130 | closedir(ProcDir); 131 | exit(EXIT_FAILURE); 132 | 133 | } 134 | 135 | /*We found the Application under /proc/pid by parsing cmdline file, cmdline has filepath as relative, 136 | * using basename get the Application name only 137 | */ 138 | 139 | char *data = malloc(512 * sizeof(char)); 140 | fgets(data,512,fp); 141 | const char *cmd = (const char *)basename (data); 142 | free(data); 143 | 144 | if(strcmp(cmd,AppName)==0){ 145 | 146 | //printf("Found Command %s %d\n", cmd, strlen(cmd)); 147 | sprintf(cmdline,"/proc/%s/statm",pDir->d_name); 148 | 149 | CheckUsage(cmdline); 150 | } 151 | 152 | fclose(fp); 153 | } 154 | 155 | 156 | } //while loop to parse directory inside proc directory 157 | closedir(ProcDirDir); 158 | 159 | } //while parse proc dir 160 | 161 | closedir(ProcDir); 162 | 163 | 164 | return EXIT_SUCCESS; 165 | } 166 | --------------------------------------------------------------------------------