From 8a902b14b143cd819d77f07de969333c2d722f68 Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Fri, 11 Oct 2024 09:05:04 -0400 Subject: [PATCH] Add explicit grid destroy when deleting Regridder (#387) Co-authored-by: raphael dussin --- CHANGES.rst | 4 ++++ xesmf/frontend.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index e4a2b5bf..d0cfa8aa 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,10 @@ What's new ========== +0.8.8 (unreleased) +------------------ +* Fix ESMpy memory issues by explictly freeing the Grid memory upon garbage collection of ``Regridder`` objects. By `Pascal Bourgault `_. + 0.8.7 (2024-07-16) ------------------ * Cast grid sizes to python's int (another Numpy 2.0 fix). (:pull:`377`) By `Pascal Bourgault `_. diff --git a/xesmf/frontend.py b/xesmf/frontend.py index 2fee239c..234af951 100644 --- a/xesmf/frontend.py +++ b/xesmf/frontend.py @@ -1109,6 +1109,12 @@ def _format_xroutput(self, out, new_dims=None): return out + def __del__(self): + # Memory leak issue when regridding over a large number of datasets with xESMF + # https://github.com/JiaweiZhuang/xESMF/issues/53 + self.grid_in.destroy() + self.grid_out.destroy() + class SpatialAverager(BaseRegridder): def __init__( @@ -1372,3 +1378,9 @@ def _format_xroutput(self, out, new_dims=None): out.coords[self._lat_out_name] = xr.DataArray(self._lat_out, dims=(self.geom_dim_name,)) out.attrs['regrid_method'] = self.method return out + + def __del__(self): + # Memory leak issue when regridding over a large number of datasets with xESMF + # https://github.com/JiaweiZhuang/xESMF/issues/53 + self.grid_in.destroy() + self.grid_out.destroy()