Skip to content

Memory leak in timedesc node order. #2423

Closed

Description

@Bai-fengting

Hello,
When I running the flowing code, I found the memroy increased very quickly (until more that 100G and I killed it).
It is wired, because in each iteration there are only one tree in process. Why the memory inceased along with the iteration.
Furthermor, when I add ts.nbytes, the memory no icreased so quickly. But the speed became slower obviously.
How I can fix that?

for i in range(0, 10000):
#    ts.nbytes
    tree_i = ts.at_index(i)
    num_lineages = {}
    count = 1
    for node in tree_i.nodes(order="timedesc"):
        if tree_i.is_sample(node):
            num_lineages[node] = tree_i.num_samples()
        else:
            num_lineages[node] = count
            count += tree_i.num_children(node) - 1

Activity

  1. jeromekelleher commented on Jul 20, 2022

    @jeromekelleher
    Member

    Hi @Bai-fengting, 👋

    You're right, this code chunk does have a serious memory leak - I'm not sure why yet, but I have reproduced it. Luckily, there's a much more efficient way to do what you want which doesn't have a memory leak:

    for tree_i in ts.trees():
        if tree_i.index == 10000:
            break
        num_lineages = {}
        count = 1
        for node in tree_i.nodes(order="timedesc"):
            if tree_i.is_sample(node):
                num_lineages[node] = tree_i.num_samples()
            else:
                num_lineages[node] = count
            count += tree_i.num_children(node) - 1

    Iterating over the trees like this is much more efficient than using ts.at() repeatedly.

  2. jeromekelleher commented on Jul 20, 2022

    @jeromekelleher
    Member

    Digging in a bit, this is something to do with using the timedesc order, which involves using self.tree_sequence.tables. Not sure why it's leaking memory so much, but there must be some reference counting going wrong somewhere.

    This is a bug, we should try and fix this specific problem with timedesc anyway even if we're not getting at the underlying problem with reference counting in tree_sequence.tables.

  3. changed the title [-]Question about memory usage.[/-] [+]Memory leak in ``timedesc`` node order.[/+] on Jul 20, 2022
  4. added
    bug
    on Jul 20, 2022
  5. jeromekelleher commented on Jul 20, 2022

    @jeromekelleher
    Member

    @Bai-fengting, I'm not so sure that my solution above is actually resolving things - can you try it out and let me know if it fixes your memory leak problem please?

  6. added this to the Python 0.5.2 milestone on Jul 20, 2022
  7. Bai-fengting commented on Jul 21, 2022

    @Bai-fengting
    Author

    @Bai-fengting, I'm not so sure that my solution above is actually resolving things - can you try it out and let me know if it fixes your memory leak problem please?

    Thanks @jeromekelleher , I tried the above code. But the problem not resolved, the memory still increased quickly.
    As you mentioned, it caused by timedesc order. After I remove order="timedesc", the memory not leak.
    In fact, I need the count the number of lineages above a specific node in each tree. I used the code mentioned in this issue:
    #2190 (comment)

    Is there other way to do that?

  8. jeromekelleher commented on Jul 21, 2022

    @jeromekelleher
    Member

    Thanks @Bai-fengting - I'm working on a fix for the leak over in #2417

    As it happens, I've also got an efficient implementation of num_lineages going over in #2422. Hopefully both of these will be released in a week or so.

    Here's a function for computing the number of lineages at a specific time from #386:

    def num_tree_lineages_preorder(tree, t):
        # Only descend the tree as far as we need to.
        lineages = 0
        stack = [tree.virtual_root]
        while len(stack) > 0:
            parent = stack.pop()
            for child in tree.children(parent):
                child_time = tree.time(child)
                if child_time > t:
                    stack.append(child)
                elif child_time <= t:
                    lineages += 1
        return lineages

    I don't think there's way to compute the num_lineages for every node without using timedesc, unfortunately.

  9. added 4 commits that reference this issue on Jul 21, 2022
    c106abe
    d655c44
    4e863d8
    01ef912
  10. added a commit that references this issue on Mar 22, 2024
    ada9596
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions