You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leak when looping through data variables of a dataset loaded from a VRT #774
@snowman2 , thanks for pointing to these options. I tired the options you suggested but they did not help to release the memory.
However, when I store the entire raster to zarr storage with to_zarr(), and load with raster = xarray.open_zarr(...), I don't see any memory leaks when iterating through the data variables. This would look like
importrioxarrayasrxrimportxarrayasxrimportgcPATH="path_to_multi_band_vrt.vrt"defno_memory_leak():
# Read from VRT and save to zarr (one chunk per band)rxr.open_rasterio(PATH, band_as_variable=True, chunks={"x": -1, "y": -1}).to_zarr(some_temp_dataset)
# Open zarr and iterate over data vars.raster=xr.open_zarr(some_temp_dataset, chunks={"x": -1, "y": -1})
bands=list(raster.data_vars)
forbandinbands:
data=raster[band].copy(deep=True).load()
deldatagc.collect()
GregoryPetrochenkov-NOAA commented on Jun 11, 2024
I have experienced similar issues with memory leaks. I did an experiment with rioxarray loading GeoTIFFs and similarly with xarray loading NetCDF files.
The tests use:
python: 3.10.14
memray: 1.12.0
rasterio: 1.3.9
xarray: 2024.3.0
rioxarray: 0.15.3
netCDF4: 1.6.5
I would run the operation 5 times and memory profile with memray as follows (run in a Jupyter Notebook):
importtimeimportsubprocessimportosimportgcfromfunctoolsimportpartialimportmemrayimportrioxarrayasrxrimportxarrayasxr%load_extmemraydefrun_test(func):
""" Driver to run memory accumulation test by running 5 times simulating a batch process """forxinrange(5):
func()
time.sleep(1)
As can be seen, as expected all memory is released by the end of the operation.
I tried changing the cache and lock arguments to no avail, I could not get rioxarray to behave similarly. The only way to fully release memory is to directly delete the objects and garbage collect:
%%memray_flamegraph--temporaldefrun_context_load_delete_gc(cand_file, bench_file, cache=False, lock=False):
""" Loads with context wrappers and deletes objects """with (rxr.open_rasterio(cand_file, cache=cache, lock=lock) asds,
rxr.open_rasterio(bench_file, cache=cache, lock=lock) asds2):
# Pure loadds.load()
ds2.load()
delds, ds2gc.collect()
run_test(partial(run_context_load_delete_gc, **rio_kwargs))
While this does work, it is not a clean solution and would necessitate prescribing users to do so. I would suggest to relabel this issue as a bug because this takes extra work for a user to diagnose. A user would not expect this behavior when loading GeoTIFFs in rioxarray. This is a dependency in a package I am supporting and the memory accumulation caused issues for workflows as can be seen in this memory profiling example:
Also, I apologize for not being able to simply paste the data as would be preferable but If you like I can provide my notebooks and data which total in about 100 MBs.
Reacted by Fernando Aristizabal
Reacted by Fernando Aristizabal, Papadopoulos Nikolas, Jacob-Shay and nkwonctrees
Activity
snowman2 commentedon May 3, 2024
Add these kwargs to
open_rasterioto disable caching:amaissen commentedon May 3, 2024
@snowman2 , thanks for pointing to these options. I tired the options you suggested but they did not help to release the memory.
However, when I store the entire raster to zarr storage with
to_zarr(), and load withraster = xarray.open_zarr(...), I don't see any memory leaks when iterating through the data variables. This would look likeGregoryPetrochenkov-NOAA commentedon Jun 11, 2024
I have experienced similar issues with memory leaks. I did an experiment with rioxarray loading GeoTIFFs and similarly with xarray loading NetCDF files.
The tests use:
I would run the operation 5 times and memory profile with memray as follows (run in a Jupyter Notebook):
I also set up these arguments in advance:
The first snippet shows rioxarray loading the two GeoTIFFs in a context wrapper which automatically calls the close method when finished:
As can be seen the memory is never released. In practice of larger iterations it leads to very large memory consumption due to accumulation of memory.
The same method is done with xarray and NetCDF files as follows:
As can be seen, as expected all memory is released by the end of the operation.
I tried changing the
cacheandlockarguments to no avail, I could not get rioxarray to behave similarly. The only way to fully release memory is to directly delete the objects and garbage collect:While this does work, it is not a clean solution and would necessitate prescribing users to do so. I would suggest to relabel this issue as a bug because this takes extra work for a user to diagnose. A user would not expect this behavior when loading GeoTIFFs in rioxarray. This is a dependency in a package I am supporting and the memory accumulation caused issues for workflows as can be seen in this memory profiling example:
Also, I apologize for not being able to simply paste the data as would be preferable but If you like I can provide my notebooks and data which total in about 100 MBs.
snowman2 commentedon Jun 21, 2024
The GDAL cache settings may be worth looking into: https://gdal.org/user/configoptions.html#performance-and-caching