Skip to content

Commit

Permalink
Add explicit grid destroy when deleting Regridder (#387)
Browse files Browse the repository at this point in the history
Co-authored-by: raphael dussin <[email protected]>
  • Loading branch information
aulemahal and raphaeldussin authored Oct 11, 2024
1 parent 5dfa90b commit 8a902b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/aulemahal>`_.

0.8.7 (2024-07-16)
------------------
* Cast grid sizes to python's int (another Numpy 2.0 fix). (:pull:`377`) By `Pascal Bourgault <https://github.com/aulemahal>`_.
Expand Down
12 changes: 12 additions & 0 deletions xesmf/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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()

0 comments on commit 8a902b1

Please sign in to comment.