Skip to content

Referring to tables in TableCollection leaks memory #2428

Closed

Description

@jeromekelleher

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

Activity

  1. added
    bug
    Python API
    on Jul 21, 2022
  2. jeromekelleher commented on Jul 21, 2022

    @jeromekelleher
    MemberAuthor

    TODO: add some reproducible example here using mprof

  3. jeromekelleher commented on Jul 21, 2022

    @jeromekelleher
    MemberAuthor

    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

  4. jeromekelleher commented on Jul 21, 2022

    @jeromekelleher
    MemberAuthor

    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.

  5. jeromekelleher commented on Jul 21, 2022

    @jeromekelleher
    MemberAuthor

    Accessing ts.tables.sites gives us:

    access-tables-sites

    🤔

  6. jeromekelleher commented on Jul 21, 2022

    @jeromekelleher
    MemberAuthor

    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.

  7. molpopgen commented on Jul 21, 2022

    @molpopgen
    Member

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

  8. jeromekelleher commented on Jul 22, 2022

    @jeromekelleher
    MemberAuthor

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

  9. jeromekelleher commented on Jul 22, 2022

    @jeromekelleher
    MemberAuthor

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

  10. jeromekelleher commented on Jul 22, 2022

    @jeromekelleher
    MemberAuthor

    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.

  11. changed the title [-]ts.tables leaks memory[/-] [+]Referring to tables in TableCollection leaks memory[/+] on Jul 25, 2022
  12. added 3 commits that reference this issue on Jul 25, 2022
    06ce8cb
    ff8756e
    676f0a6
  13. added a commit that references this issue on Jul 27, 2022
    06285a4
  14. added a commit that references this issue on Mar 22, 2024
    8e87bfd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Python APIbug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions