now detect memory leak open/closing files with tst_mem1.c
#include <err_macros.h>
#include <sys/resource.h>
+#define FILE_NAME "tst_mem1.nc"
+#define NUM_FILE_OPENS 100
+
int main()
{
- int dataset_id, time_id, dummyvar_id, ret, idx;
+ int dataset_id, time_id, dummyvar_id, idx;
size_t start[1] = {0};
size_t count[1] = {100};
double data[100];
@@ -27,29 +30,27 @@ int main()
data[idx]=-99;
};
- printf("\n*** Testing netcdf-4 memory use with unlimited dimensions.\n");
- printf("*** testing with user-contributed code...");
+ printf("\n*** Testing netcdf-4 memory use.\n");
+ printf("*** testing mem use opening/closing file...");
+ {
+ long my_rss = 0;
+
+ if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &dataset_id)) ERR;
+ if (nc_def_dim(dataset_id, "time", NC_UNLIMITED, &time_id)) ERR;
+ if (nc_def_var(dataset_id, "dummy", NC_DOUBLE, 1, &time_id, &dummyvar_id)) ERR;
+ if (nc_put_vara(dataset_id, dummyvar_id, start, count, data)) ERR;
+ if (nc_close(dataset_id)) ERR;
+
+ for (idx = 0; idx < NUM_FILE_OPENS; idx++)
{
- if (nc_create("test.nc", NC_CLOBBER | NC_NETCDF4, &dataset_id)) ERR;
- /*ret=nc_create("test.nc", NC_CLOBBER | NC_64BIT_OFFSET, &dataset_id);*/
- if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
- ret=nc_def_dim(dataset_id, "time", NC_UNLIMITED, &time_id);
- if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
- ret=nc_def_var(dataset_id, "dummy", NC_DOUBLE, 1, &time_id, &dummyvar_id);
- if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
- /*ret=nc_enddef(dataset_id);*/
- ret=nc_put_vara(dataset_id, dummyvar_id, start, count, data);
- if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
- ret=nc_close(dataset_id);
- if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
-
- for (idx = 0; idx < 100000; idx++) {
- ret=nc_open("test.nc", NC_NOWRITE, &dataset_id);
- if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
- ret=nc_close(dataset_id);
- if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
+ if (nc_open(FILE_NAME, NC_NOWRITE, &dataset_id)) ERR;
+ if (nc_close(dataset_id)) ERR;
getrusage(RUSAGE_SELF, &r_usage);
- /* printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss); */
+ printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss);
+ if (!my_rss)
+ my_rss = r_usage.ru_maxrss;
+ else
+ if (my_rss != r_usage.ru_maxrss) ERR;
};
}
SUMMARIZE_ERR;
