-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
High memory usage #53
Comments
Thanks for reporting this issue. I've been wanting to diagnose the memory problem for a long time, and have just taken a closer look at this. As background knowledge, ESMPy relies on the explicit The higher-level If you use More detailsTo demonstrate that the memory issue comes from the underlying ESMPy calls, consider this """A minimum script to test ESMPy memory allocation."""
import numpy as np
import ESMF
from memory_profiler import profile
def create_grid(shape):
grid = ESMF.Grid(np.array(shape),
staggerloc = ESMF.StaggerLoc.CENTER,
coord_sys = ESMF.CoordSys.SPH_DEG)
return grid
def fill_grid(grid, lons, lats):
lon_pointer = grid.get_coords(coord_dim=0,
staggerloc=ESMF.StaggerLoc.CENTER)
lat_pointer = grid.get_coords(coord_dim=1,
staggerloc=ESMF.StaggerLoc.CENTER)
lon_pointer[:] = lons
lat_pointer[:] = lats
@profile
def test_esmpy():
# define test grids
lons_in, lats_in = np.meshgrid(
np.arange(-120, 120, 0.4),
np.arange(-60, 60, 0.3)
)
lons_out, lats_out = np.meshgrid(
np.arange(-120, 120, 0.6),
np.arange(-60, 60, 0.4)
)
# build ESMPy regridder
sourcegrid = create_grid(lons_in.shape)
destgrid = create_grid(lons_out.shape)
fill_grid(sourcegrid, lons_in, lats_in)
fill_grid(destgrid, lons_out, lats_out)
sourcefield = ESMF.Field(sourcegrid)
destfield = ESMF.Field(destgrid)
regrid = ESMF.Regrid(sourcefield, destfield, filename=None,
regrid_method=ESMF.RegridMethod.BILINEAR,
unmapped_action=ESMF.UnmappedAction.IGNORE)
# release underlying Fortran memory
sourcegrid.destroy()
destgrid.destroy()
sourcefield.destroy()
destfield.destroy()
regrid.destroy()
# de-reference Python objects
sourcegrid = None
destgrid = None
sourcefield = None
destfield = None
regrid = None
lons_in = None
lats_in = None
lons_out = None
lats_out = None
if __name__ == '__main__':
test_esmpy()
The I am going to test the new module-level |
So it seems like I added this extra code to the end of my original test script: mg = ESMF.Manager()
mg.__del__() Then,
So This top-level destroy also has serious side-effect: later attempts to build new regridders will lead to |
Still, my current suggestion is to restart the kernel and load existing weights, if memory usage becomes a problem. I will need to check with the ESMF team on the proper use of |
How do we restart the kernel with the xESMF API? Or should that not leak memory? |
I mean restart Python kernel, and set |
That doesn't seem to behave as I expected, it still seems the regridder is never free'd.
|
@Plantain Remove the first |
@JiaweiZhuang @Plantain curious if any more work has been done on this. We just encountered this issue when trying to run repeated tasks using different regridders with |
The memory use increases by how much? With #75 should completely solve this problem. The new |
Here's an example where I load a series of regridder files and then go back to the first regridder file. And the memory use keeps expanding (for the most part). Does this seem unexpected to you?:
|
Interesting that line 14 has no memory increment. If it is an ESMF memory leak, there should be a steady increment. The problem might be related to uncleaned ESMF objects, or Garbage collection on numpy seems a tricky issue itself, and If there is still problem after #75 is implemented, then it will be an numpy/scipy/xarray issue that is out of my control. |
Hi. Is there any news regarding this issue? Our application needs to perform regriding many times, and we have tracked that each usage of the Regridder causes a massive increase in memory usage, which is not released. Has this issue come to any resolution? Thanks |
I just became aware of this issue, and thought I would chime in from the ESMPy perspective (ESMPy is the engine behind xESMF). The ESMF 8.1.0 release, expected at the end of March '21, will include a fix for a memory leak in the search algorithm of the regridding code. This may resolve the memory issues discussed in this thread. There should be a new conda package version of ESMPy 8.1.0 by the first of April. |
If people are still having memory issues with xesmf running over a large number of datasets (and likely parallelized, which means the regridder needs to be loaded in each process as it can't be pickled)... I was able to solve this issue in my case by adding the following after the regridder object was no longer needed within the context...
where regridder is an xesmf.Regridder object. Not sure if the last del command is required (it certainly didn't work in isolation), but it doesn't hurt to keep it... |
The memory usage of xESMF seems quite high, higher than standalone ESMF CLI, and I wonder if this is due to a possible leak, as loading from a saved weights file uses much less RAM? Additionally, perhaps it can be improved by using lower precision (32bit vs 64bit).
I also note it doesn't seem possible to destroy a Regridder object to free memory?
The text was updated successfully, but these errors were encountered: