Skip to content
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

Potential memory leak using lightkurve.read #1388

Closed
zclaytor opened this issue Nov 18, 2023 · 3 comments · Fixed by #1390
Closed

Potential memory leak using lightkurve.read #1388

zclaytor opened this issue Nov 18, 2023 · 3 comments · Fixed by #1390

Comments

@zclaytor
Copy link

Problem description

I am using lightkurve.read to read the Kepler Bonus light curves from my filesystem. The total memory used increases with each new file, even as the variable the light curve is read into is overwritten. After reading a few hundred light curves, not even doing any light curve operations, several GB of memory are used. The behavior persists even when I delete the light curve after reading it in, and when forcing garbage collection.

Example

from glob import glob
import tracemalloc
import lightkurve as lk
# import gc

# files is a list of 4211 KeplerLightCurve fits file paths.
files = sorted(glob("kbonus-bkg/lcs/0033/*/*fits"))
tracemalloc.start()

for i, f in enumerate(files):
    l = lk.read(f)
    # print memory used so far in MB
    a, b = tracemalloc.get_traced_memory()
    print(i, a/1e6, b/1e6)
    # del l
    # gc.collect()

prints the following to the terminal:

0 29.884892 38.138305
1 37.926699 49.275103
2 56.568039 68.800881
3 53.747654 68.800881
4 49.065743 68.800881
5 68.433916 75.703975
6 87.464786 99.720875
7 80.419431 99.720875
8 106.513734 116.28036
9 113.602286 127.28021
10 101.602975 127.28021
11 104.040992 127.28021
12 129.269737 138.434605
13 114.449967 138.434605
14 112.08688 138.434605
15 134.411911 141.053458
...

Expected behavior

I expect the total memory usage to increase for the first few files, but then level off as memory is freed up and reallocated. This is the output when I replace lk.read with astropy.table.Table.read:

0 5.248844 5.645178
1 4.370564 9.53727
2 5.389507 9.749385
3 3.378593 9.749385
4 1.111893 9.749385
5 4.37702 9.749385
6 5.494544 9.827411
7 1.866013 9.827411
8 5.481917 9.827411
9 5.695968 11.061116
10 1.07941 11.061116
11 1.209794 11.061116
12 5.440224 11.061116
13 0.551675 11.061116
14 0.551974 11.061116
15 4.367649 11.061116

Here, the maximum memory used never goes above about 11 GB.

Environment

  • platform: Red Hat Enterprise Linux 8.8 (Ootpa)
  • lightkurve version: 2.4.2 (installed using conda -c conda-forge) and 2.5.0dev (installed using pip from source in the cloned repository)
@orionlee
Copy link
Collaborator

  1. I can reproduce problem with TESS SPOC 2 minute lightcurves on Windows, so it is not specific to Kepler Bonus lightcurves / Linux.
  2. the problem seems to be a recent regression. The older lightkurve 2.4.0 does not appear to have the memory leak

The problem exists for TESS SPOC lightcurves:

lightkurve 2.5.0dev
astropy 5.0.4
0 8.715249 12.326077
1 11.507044 17.704528
2 13.629003 19.827207
3 17.910356 24.108518
4 18.42802 24.631339
5 21.177316 27.380927
6 25.452262 31.656041
7 26.010972 32.214751
8 28.761221 34.965
9 33.035526 39.239305
10 33.597524 39.801303
...
100 221.514371 227.757565
101 224.226204 230.469398
102 228.460606 234.7038
103 230.504471 236.747665
104 233.216248 239.459442
105 237.450479 243.693673
...
146 318.471775 324.392998
147 318.974317 324.886945
148 321.538435 327.451063
149 324.656307 329.973692
150 325.023099 329.973692

But the slightly older lightkurve v2.4.0 does not have the problem:

lightkurve 2.4.0
astropy 5.0.4
0 6.542636 10.153735
1 7.216337 13.414637
2 7.222926 13.422954
3 9.420149 15.620135
4 7.856869 15.620135
...
119 40.648573 46.893591
120 36.930138 46.893591
121 39.122917 46.893591
...
197 35.755834 46.893591
198 36.766859 46.893591
199 7.098715 46.893591
...

@orionlee
Copy link
Collaborator

PR #1299 seems to have introduced the problem.

Workarounds:

  1. Downgrade lightkurve to v2.4.0, or
  2. if you use the source, you can change the few lines to use the old v2.4.0 logic

For

else:
with fits.open(filename) as hdulist:
hdulist = deepcopy(hdulist)

Replace it with

    else:
        hdulist = fits.open(filename)

Essentially, it revert back to the old logic. The old logic has some issues. But I think they mostly manifest in edge cases.

I don't understand why the new logic cause memory leak though.

@orionlee
Copy link
Collaborator

Confirmed that the memory leak indeed stems from the deecopy(hdulist) in the PR.

A sample script that demonstrates the problem.

from glob import glob
import tracemalloc
import astropy
from astropy.io import fits
import lightkurve as lk
from copy import deepcopy

# import gc

# Show lk.read memory leak issues in
# https://github.com/lightkurve/lightkurve/issues/1388

print('lightkurve',  lk.__version__)
print('astropy', astropy.__version__)


def read_lite(filename):
    # simulate the logic in 
    # https://github.com/lightkurve/lightkurve/pull/1299
    hdulist = None
    with fits.open(filename) as hdulist:
        hdulist = deepcopy(hdulist)
    return hdulist

    
# files is a list of sample SPOC TESS lc
files = sorted(glob("spoc_samples/*_lc.fits"))
tracemalloc.start()

for i, f in enumerate(files[:]):
    l = read_lite((f))
    # print memory used so far in MB
    a, b = tracemalloc.get_traced_memory()
    print(i, a / 1e6, b / 1e6)
    # del l
    # gc.collect()

Similar memory leak:

lightkurve 2.5.0dev
astropy 5.0.4
0 2.450405 4.575374
1 4.578252 6.877436
2 6.697511 8.996654
3 8.81539 11.115467
4 10.934333 13.232662
...
145 297.996694 300.220553
146 300.038592 302.264887
147 302.08049 304.306785
148 304.122388 306.348683
149 306.164286 308.390581

orionlee added a commit to orionlee/lightkurve that referenced this issue Nov 23, 2023
orionlee added a commit to orionlee/lightkurve that referenced this issue Nov 23, 2023
orionlee added a commit that referenced this issue Dec 5, 2023
* Fixed memleak for lc in #1388

* Fixed memleak for tpf in #1388

* add test for read HDUList

* Explicit tests for read memory leaks (LC & TPF)
- Run in memtest workflow in CI (pytest -m memtest --remote-data)

* Test tpf.from_fits_images() to ensure no unclosed file handles

* Revert lc.hdu change in PR #1299

* Revert raising ResourceWarning as error during tests in PR #1299
- For it to actually work (to ensure no unclosed files), "error::pytest.PytestUnraisableExceptionWarning" wil also be needed
- but it'll create many false alarms.
- Explicit tests on unclosed file handles is done in specific tests instead.

* add changelog  [skip ci]
danhey added a commit to danhey/lightkurve that referenced this issue Dec 13, 2023
commit 5e4c619
Author: Sam Lee <orionlee@users.noreply.github.com>
Date:   Tue Dec 5 08:21:02 2023 -0800

    pytests: isolate astropy cache from user defaults (lightkurve#1391)

commit eabc909
Author: Sam Lee <orionlee@users.noreply.github.com>
Date:   Tue Dec 5 08:20:24 2023 -0800

    Support QLP changes in sectors 56+ (lightkurve#1392)

    * QLP sector 56+: handle default flux_err column

    * handle QLP-specific quality bitmask

    * docstring updates for QLP sectors 56+

    * add changelog [skip ci]

commit 68fdf03
Author: Sam Lee <orionlee@users.noreply.github.com>
Date:   Tue Dec 5 08:14:18 2023 -0800

    Fix memory leak in reading LC/TPF  (lightkurve#1390)

    * Fixed memleak for lc in lightkurve#1388

    * Fixed memleak for tpf in lightkurve#1388

    * add test for read HDUList

    * Explicit tests for read memory leaks (LC & TPF)
    - Run in memtest workflow in CI (pytest -m memtest --remote-data)

    * Test tpf.from_fits_images() to ensure no unclosed file handles

    * Revert lc.hdu change in PR lightkurve#1299

    * Revert raising ResourceWarning as error during tests in PR lightkurve#1299
    - For it to actually work (to ensure no unclosed files), "error::pytest.PytestUnraisableExceptionWarning" wil also be needed
    - but it'll create many false alarms.
    - Explicit tests on unclosed file handles is done in specific tests instead.

    * add changelog  [skip ci]

commit f8e8c16
Author: Christina Hedges <christina.l.hedges@nasa.gov>
Date:   Fri Nov 3 11:12:48 2023 -0400

    updating to v2.5.0dev [skip ci]

commit 1a6b7c2
Author: Christina Hedges <christina.l.hedges@nasa.gov>
Date:   Fri Nov 3 11:02:06 2023 -0400

    release v2.4.2 [skip ci]

commit 47cbfcf
Author: Christina Hedges <christina.l.hedges@nasa.gov>
Date:   Fri Nov 3 11:01:05 2023 -0400

    releasing v2.4.2

commit e3bd292
Author: Christina Hedges <14965634+christinahedges@users.noreply.github.com>
Date:   Fri Nov 3 01:59:04 2023 -0400

    Revert "Update the stylefile 💅 (lightkurve#1311)" (lightkurve#1382)

    This reverts commit 3b9a0af.

commit 7be1a0f
Author: Christina Hedges <14965634+christinahedges@users.noreply.github.com>
Date:   Thu Nov 2 20:51:37 2023 -0400

    fix changelog and version number

commit 3b9a0af
Author: Daniel <38233719+danhey@users.noreply.github.com>
Date:   Thu Nov 2 10:33:31 2023 -1000

    Update the stylefile 💅 (lightkurve#1311)

    * update stylefile

    * mark edge colors

    * changed lightkurve plotting style

    * update merge conflict [skip ci]

    ---------

    Co-authored-by: Christina Hedges <christina.l.hedges@nasa.gov>

commit 7a7dc2a
Author: Sam Lee <orionlee@users.noreply.github.com>
Date:   Thu Nov 2 12:02:17 2023 -0700

    Fix download error due to no dataURL in MAST result (lightkurve#1380)

    * Fix downlod error due to missing dataURL in MAST result.

    * handle changes in MAST for some Kepler search due to extra KBONUS-BKG
    - some (but not all) Kepler search returns an extra KBONUS-BKG

    * fixing searchresult ordering and HLSP URL

    * update changelog [skip ci]

    ---------

    Co-authored-by: Christina Hedges <christina.l.hedges@nasa.gov>

commit b098e81
Author: Rebekah <rebekahhounsell@gmail.com>
Date:   Thu Nov 2 14:28:28 2023 -0400

    Changed flux_raw to flux_corr for TASOC files (lightkurve#1333)

    * updated to corr

    * trying to re-initiate checks

commit ce610e8
Author: Nschanche <nschanch@umd.edu>
Date:   Thu Nov 2 14:23:19 2023 -0400

    Update searching-for-data-products (lightkurve#1370)

    Modified text to reflect search results

commit dde5582
Author: H. Arda Güler <80536083+arda-guler@users.noreply.github.com>
Date:   Thu Nov 2 21:22:48 2023 +0300

    Remove redundant conditional (lightkurve#1374)

    in regressioncorrector.py

commit 4f2dbed
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Thu Oct 5 10:31:37 2023 -0400

    Bump actions/checkout from 3 to 4 (lightkurve#1367)

    Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
    - [Release notes](https://github.com/actions/checkout/releases)
    - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
    - [Commits](actions/checkout@v3...v4)

    ---
    updated-dependencies:
    - dependency-name: actions/checkout
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...

    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit 382fd3a
Author: Christina Hedges <christina.l.hedges@nasa.gov>
Date:   Wed Sep 6 09:43:50 2023 -0400

    fix log

commit 8b24061
Author: Nschanche <nicole.e.schanche@nasa.gov>
Date:   Tue Sep 5 17:45:42 2023 -0400

    Updated filename check to fix issue lightkurve#1358 (lightkurve#1364)

commit 5a1d1d1
Author: Christina Hedges <14965634+christinahedges@users.noreply.github.com>
Date:   Tue Sep 5 17:45:01 2023 -0400

    Jupyterhub support (lightkurve#1363)

    * Modify show/interact functions to automatically supply a callable
    for the notebook_url parameter to adapt to operating behind the a
    JupyterHub proxy with randomly generated ports for the Bokeh server.
    ---------

    Co-authored-by: jaytmiller <jmiller@stsci.edu>

commit 6176eb0
Author: Christina Hedges <christina.l.hedges@nasa.gov>
Date:   Tue Sep 5 15:17:26 2023 -0400

    fix numpy bug in search

commit 7d485b6
Author: Zé Vinícius <jvmirca@gmail.com>
Date:   Wed Aug 23 04:42:53 2023 +0800

    Raise a RuntimeError in case arclength cant be computed (lightkurve#1331)

    * check if arclength can be computed, else raise an error

    * handle both quantity and numpy arrays

    ---------

    Co-authored-by: Christina Hedges <christina.l.hedges@nasa.gov>

commit e751c16
Author: Christina Hedges <14965634+christinahedges@users.noreply.github.com>
Date:   Tue Aug 22 16:42:35 2023 -0400

    fixing CDIPs stitching bug (lightkurve#1361)

commit 394246f
Author: Zé Vinícius <jvmirca@gmail.com>
Date:   Wed Aug 23 03:41:00 2023 +0800

    Expose n_iters to the pca method in DesignMatrix (lightkurve#1334)

    * expose n_iters from fbpca so that users can control accuracy of optimality

    * Update src/lightkurve/correctors/designmatrix.py

    Co-authored-by: Dan Foreman-Mackey <dfm@dfm.io>

    * updated changelog

    ---------

    Co-authored-by: Dan Foreman-Mackey <dfm@dfm.io>
    Co-authored-by: Christina Hedges <christina.l.hedges@nasa.gov>

commit edcdd65
Author: Daniel <38233719+danhey@users.noreply.github.com>
Date:   Tue Aug 22 21:13:39 2023 +0200

    fix outlier removal bug (lightkurve#1313)

    * fix outlier removal bug

    * update test

    * add nan to test

    * update comment

    ---------

    Co-authored-by: Christina Hedges <14965634+christinahedges@users.noreply.github.com>

commit 8962d85
Author: Christina Hedges <14965634+christinahedges@users.noreply.github.com>
Date:   Tue Aug 22 15:12:38 2023 -0400

    Updated lightkurve aperture functions to be compliant with numpy 1.25.0 (lightkurve#1360)

    * updated aperture function

    * fixing @Nschanche comments [skip ci]

    * update changelog [skip ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants