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 6001059
In Process

The Archive Base Latest Questions

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

I found a problem with exec (It happened in a system that has to

  • 0

I found a problem with exec (It happened in a system that has to be extensible with user written scripts). I could reduce the problem itself to this code:

def fn():
    context = {}
    exec '''
class test:
    def __init__(self):
        self.buf = '1'*1024*1024*200
x = test()''' in context

fn()

I expected that memory should be freed by the garbage collector after the call of function fn. However, the Python process still consumes the additional 200MB of memory and I have absolutely no clue what is happening here and how to release the allocated memory manually.

I suspect that defining a class inside exec is not a very bright idea, but, first of all, I want to understand what is going wrong in the example above.

It looks like wrapping class instance creation in another function solves the problem but what is the difference?

def fn():
    context = {}
    exec '''
class test:
    def __init__(self):
        self.buf = '1'*1024*1024*200
def f1(): x = test()
f1()
    ''' in context
fn()

This is my Python interpreter version:

$ python
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00) 
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
  • 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-23T00:46:56+00:00Added an answer on May 23, 2026 at 12:46 am

    The reason that you’re seeing it take up 200Mb of memory for longer than you expect is because you have a reference cycle: context is a dict referencing both x and test. x references an instance of test, which references test. test has a dict of attributes, test.__dict__, which contains the __init__ function for the class. The __init__ function in turn references the globals that it was defined with — which is the dict you passed to exec, context.

    Python will break these reference cycles for you (since nothing involved has a __del__ method) but it requires gc.collect() to run. gc.collect() will run automatically every N allocations (determined by gc.set_threshold()) so the “leak” will go away at some point, but if you want it to go away immediately you can run gc.collect() yourself, or break the reference cycle yourself before exiting the function. You can easily do the latter by calling context.clear() — but you should realize that that affects all instances of the class you created in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run into the same problem as found in this question . However, I
Ok, so I'm quite new to jQuery, but found this bizzare problem just now,
I tried googling for this issue and found many people with the same problem
I tried googling this and I haven't found an answer yet to my problem.
I'm creating a process on Windows from Java. My problem is that this process
I found this function that does an AWESOME job (IMHO): http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl /** * Get
edit 2: I found the problem. The quick answer is that the lack of
I found a comment that hints at a problem I think I'm seeing, in
After having written a few helper classes in magento, now i have this problem,
I'm surprised that this hasn't come up before but I haven't found anything in

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.