[IronPython] Garbage collection of imported modules

Christian Muirhead christian.muirhead at resolversystems.com
Fri Jul 25 19:28:42 CEST 2008

Hi guys -

If I execute the following:

from System.Diagnostics import Process

M = 1024 * 1024

import bigmodule

for i in range(1000):
     m = reload(bigmodule)
     print i, Process.GetCurrentProcess().PrivateMemorySize64 / M

(in IP 1.1.1), I see steadily increasing memory usage - the assemblies 
created by reloading each module are not being garbage collected. 
Talking to Giles (the boss here), he remembered a conversation with 
someone at the Languages.NET conference about how some assemblies aren't 
collectable, while others that are generated without debugging 
information can be collected.

Looking at the source for PythonEngine I can see the 
EngineOptions.ClrDebuggingEnabled property, with a comment saying 
essentially the same thing.

However, when I try to use this with the following code, I still get 
increasing memory usage.

from textwrap import dedent
from IronPython.Hosting import PythonEngine, EngineOptions
from System.Diagnostics import Process

M = 1024 * 1024

o = EngineOptions()
o.ClrDebuggingEnabled = False
p = PythonEngine(o)

for i in range(1000):
     import bigmodule
     print i, Process.GetCurrentProcess().PrivateMemorySize64 / M

So the assemblies generated from compiling the imported module are still 
not collectable. (I've also tried running the first script in an 
interpreter (with ClrDebuggingEnabled off) embedded in a C# application, 
to make sure there's nothing untoward happening because of nested 
PythonEngines, but I see the same behaviour).

Is there any way to have these modules collected? Is the situation the 
same in IP 2.*?


Christian Muirhead
Resolver Systems
christian.muirhead at resolversystems.com
We're hiring! http://www.resolversystems.com/jobs/

Resolver Systems Ltd
17a Clerkenwell Road, London EC1M 5RD, UK
VAT No.: GB 893 5643 79 Registered in England and Wales as company 
number 5467329.
Registered address: 843 Finchley Road, London NW11 8NA, UK

More information about the Ironpython-users mailing list