I have a code that periodically writes output results data to a NetCDF-4 file (with HDF5 underneath). If I run the program using NetCDF-3 output, I get no memory growth, but if I use NetCDF-4, I get continual memory growth proportional to the number of output steps.
At each output step, I write the same amount of data. There are no memory leaks, so it looks like there is a buffer or cache that is growing over time. I do “flush” the data after each output step. I did run this with valgrind --massif
and it does show the memory growth over time. Here is an output near the beginning of the program showing the largest memory block:
98.62% (20,747,915B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->82.85% (17,431,077B) 0x7E48714: H5MM_malloc (H5MM.c:292)
| ->80.84% (17,007,799B) 0x7DB51ED: H5FL_malloc (H5FL.c:243)
| | ->68.41% (14,392,479B) 0x7DB6024: H5FL_blk_malloc (H5FL.c:921)
| | | ->59.92% (12,605,504B) 0x7CFE7AF: H5D__chunk_mem_alloc (H5Dchunk.c:1363)
| | | | ->59.90% (12,601,344B) 0x7D03AC9: H5D__chunk_lock (H5Dchunk.c:3587)
| | | | | ->59.90% (12,601,344B) 0x7D013D9: H5D__chunk_write (H5Dchunk.c:2392)
| | | | | ->59.90% (12,601,344B) 0x7D2E640: H5D__write (H5Dio.c:817)
| | | | | ->59.90% (12,601,344B) 0x7D2C4FE: H5Dwrite (H5Dio.c:335)
| | | | | ->59.90% (12,601,344B) 0x777CCAC: NC4_put_vars (hdf5var.c:1739)
| | | | | ->59.90% (12,601,344B) 0x777C047: NC4_put_vara (hdf5var.c:1325)
| | | | | ->59.90% (12,601,344B) 0x7716AC0: NC_put_vara (dvarput.c:97)
| | | | | ->59.90% (12,601,344B) 0x7717F42: nc_put_vara_double (dvarput.c:711)
And here is a similar dump near the end of the program showing the same traceback and much higher memory usage:
99.50% (102,556,387B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->96.28% (99,243,541B) 0x7E48714: H5MM_malloc (H5MM.c:292)
| ->95.04% (97,965,223B) 0x7DB51ED: H5FL_malloc (H5FL.c:243)
| | ->89.29% (92,029,991B) 0x7DB6024: H5FL_blk_malloc (H5FL.c:921)
| | | ->81.86% (84,380,616B) 0x7CFE7AF: H5D__chunk_mem_alloc (H5Dchunk.c:1363)
| | | | ->81.86% (84,372,296B) 0x7D03AC9: H5D__chunk_lock (H5Dchunk.c:3587)
| | | | | ->81.86% (84,372,296B) 0x7D013D9: H5D__chunk_write (H5Dchunk.c:2392)
| | | | | ->81.86% (84,372,296B) 0x7D2E640: H5D__write (H5Dio.c:817)
| | | | | ->81.86% (84,372,296B) 0x7D2C4FE: H5Dwrite (H5Dio.c:335)
| | | | | ->81.86% (84,372,296B) 0x777CCAC: NC4_put_vars (hdf5var.c:1739)
| | | | | ->81.86% (84,372,296B) 0x777C047: NC4_put_vara (hdf5var.c:1325)
| | | | | ->81.86% (84,372,296B) 0x7716AC0: NC_put_vara (dvarput.c:97)
| | | | | ->81.86% (84,372,296B) 0x7717F42: nc_put_vara_double (dvarput.c:711)
Each output step should write about 186,480 bytes of “bulk” data (10 calls with 10,648 bytes + 10 calls with 8,000 bytes) and there are around 1,000 steps in this run. The first trace above should be near the first few output steps and the last trace is near the end of the run.
Is this an expected result due to buffering of the data? If so, is there a way to “flush” the buffer or reduce the buffer size. It does look like the memory growth does “top out” part way through the run; here is massif’s representation of memory use over time:
MB
98.30^ #
| @::::::::@@::@:::::@::@:::::@:::::@#:
| :::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| ::::@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| ::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @@@@@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| :@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| :@:@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| ::@:@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @@::@:@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @:@@::@:@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| :@:@@::@:@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| @:@:@@::@:@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
| :@@:@:@@::@:@@ @@@:::@:: :@::@:::@@::::::::@ ::@:::::@::@:::::@:::::@#:
0 +----------------------------------------------------------------------->Gi
0 7.061
Any insights would be welcome.
Thanks,
…Greg