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

Referring to tables in TableCollection leaks memory #2428

Closed
jeromekelleher opened this issue Jul 21, 2022 · 9 comments · Fixed by #2439
Closed

Referring to tables in TableCollection leaks memory #2428

jeromekelleher opened this issue Jul 21, 2022 · 9 comments · Fixed by #2439
Labels
bug Something isn't working Python API Issue is about the Python API
Milestone

Comments

@jeromekelleher
Copy link
Member

See analysis in #2424 for details, and e.g., #2427.

@jeromekelleher jeromekelleher added bug Something isn't working Python API Issue is about the Python API labels Jul 21, 2022
@jeromekelleher jeromekelleher added this to the Python 0.5.3 (paper) milestone Jul 21, 2022
@jeromekelleher
Copy link
Member Author

TODO: add some reproducible example here using mprof

@jeromekelleher
Copy link
Member Author

Let's mimic the memory leak we see in ts.site(position=x) by accessing the relevant bits of the tables in a loop:

import msprime
import time

before = time.perf_counter()
ts = msprime.sim_ancestry(
    10000,
    sequence_length=1000000,
    population_size=10_000,
    recombination_rate=1e-8,
    random_seed=1234,
)
ts = msprime.sim_mutations(ts, rate=1e-7, random_seed=1)
print(ts)
duration = time.perf_counter() - before
print(f"Simulation of {ts.num_trees} trees done after {duration:.2f} seconds")

for site in ts.sites():
    # pos = ts.tables.sites.position
    tables = ts.tables
    if site.id >= 1000:
        break

First, just accessing ts.tables like here:

access-tables

As it should be

@jeromekelleher
Copy link
Member Author

Now, change it around so we do pos=ts.tables.sites.position:

access-tables-sites-position

We're using waaaaay more memory, and it looks like garbage collection isn't recovering it all.

@jeromekelleher
Copy link
Member Author

Accessing ts.tables.sites gives us:

access-tables-sites

🤔

@jeromekelleher
Copy link
Member Author

It seems like there must be some circular references down in where we do the allocation of the Table instances. It's all quite complicated and twisty down there though, so I'm at a bit of a loss as to what's going on here.

@molpopgen
Copy link
Member

We've got a couple of these piling up. Relevant to tskit-dev/msprime#1946?

@jeromekelleher
Copy link
Member Author

Yep, that's probably the same thing, thanks @molpopgen

@jeromekelleher
Copy link
Member Author

Note: #2080 is relevant here, because we create a new instance of the high-level table class each time tables.nodes is accessed.

@jeromekelleher
Copy link
Member Author

It seems this is partially at least because of how we're doing metadata decoding. If I run the code accessing ts.tables.sites above (and therefore creating a new instance of SiteTable each time because of #2080) then we get memory usage going up to 1GB like above. However, if I comment out this line here:

self.row_class = row_class

Usage goes down to a nice flat 60MiB. So it seems like we've created a circular reference of some sort by wrapping the row_class, and so we leak some memory every time we access one of the tables.

@jeromekelleher jeromekelleher changed the title ts.tables leaks memory Referring to tables in TableCollection leaks memory Jul 25, 2022
jeromekelleher added a commit to jeromekelleher/tskit that referenced this issue Jul 25, 2022
Also simplify the class structure of Tables

Closes tskit-dev#2428
jeromekelleher added a commit to jeromekelleher/tskit that referenced this issue Jul 25, 2022
Also simplify the class structure of Tables

Closes tskit-dev#2428
jeromekelleher added a commit to jeromekelleher/tskit that referenced this issue Jul 27, 2022
Also simplify the class structure of Tables

Closes tskit-dev#2428
@mergify mergify bot closed this as completed in 8e87bfd Jul 27, 2022
jeromekelleher added a commit to jeromekelleher/tskit that referenced this issue Jul 27, 2022
Also simplify the class structure of Tables

Closes tskit-dev#2428
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Python API Issue is about the Python API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants