Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 249075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:23:46+00:00 2026-05-11T21:23:46+00:00

I am working on a library that loads files (hfd5 – pytables) into an

  • 0

I am working on a library that loads files (hfd5 – pytables) into an object structure. The actual classes being used for the structure is loaded as a string from the hdf5 file, and then
loaded in this fashion:

class NamespaceHolder(dict):
    # stmt is the source code holding all the class defs
    def execute(self, stmt):
        exec stmt in self

The problem is, loading multiple classes like this, causes objects to appear in the uncollectible part of the garbage collection, namely the actual class definitions. I can also load this into a global dictionary, but the problem remains of orphaned classes. Is there any way to unload the classes?

The main problem is the class.mro attribute, which contains a reference back to the class itself, causing circular references that the garbage collector can’t handle.

Here is a small test case to see for yourselves:

import gc

if __name__ == "__main__":
    gc.enable()
    gc.set_debug(gc.DEBUG_LEAK)

    code = """
class DummyA(object):
    pass
"""
    context = {}

    exec code in context
    exec code in context

    gc.collect()
    print len(gc.garbage)

Just a note: I have already argued against using parsing off text in a file for creating classes earlier, but apparently they are set on using it here and see some benefits I don’t, so going away from this solution isn’t feasible now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:23:46+00:00Added an answer on May 11, 2026 at 9:23 pm

    The gc.set_debug(gc.DEBUG_LEAK) causes the leak. Try this:

    import gc
    
    def foo():                              
        code = """
    class DummyA(object):
        pass             
    """
        context = {}
        exec code in context
        exec code in context
    
        gc.collect()
        print len(gc.garbage), len(gc.get_objects())
    
    gc.enable()
    foo(); foo() # amount of objects doesn't increase
    gc.set_debug(gc.DEBUG_LEAK)
    foo() # leaks
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Python library that interfaces with a web service API. Like
I am working on a very large scale computing library that is using STL
I am working in a project that has two main parts: a class library
I am working on a client-server application that uses boost::serialization library for it's serialization
I'm working on a source-code visualization project that uses the Processing core library. The
I am attempting to load document files into a document library in SharePoint using
I'm working on a Java library and would like to remove some functions from
I'm working on a piece of library code around IDisposable . The managed path
I have been working on a WCF service library where hopefully all the business
The current class library I am working on will have a base class (Field)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.