I am fairly sure it is no different than when using disposable objects in any 
.NET application. If you don’t call IDisposable.Dispose() directly (or via the 
“using()” statement), then those objects will be disposed via their finalizer 
when the GC collects them. This happens automatically and fairly frequently in 
a .NET application. Those objects will be cleaned up for you – assuming you 
really aren’t holding references to them. If you find they never seem to go 
away, then I highly recommend attaching a memory profiler (SciTech’s 
MemProfiler is superb) and exploring the heap graph for the root paths that are 
responsible for pinning them.

Take note that your ScriptScope itself is holding references to all of the 
objects contained within. You will need to discard all references to that 
ScriptScope if you want it (and its contents) to be collected by the runtime.

If you conclude that they truly are eligible for collection, and you just can’t 
wait for the system to clean up on its own, then you can always call 
GC.Collect() directly. I really don’t recommend that kind of brute force 
approach though. There are very few situations where calling GC.Collect() is 
really the right thing to do.


Keith Rome
Senior Consultant and Architect
MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
Wintellect | 770.617.4016 | kr...@wintellect.com<mailto:r...@wintellect.com>
www.wintellect.com<http://www.wintellect.com/>

From: ironpython-users-bounces+rome=wintellect....@python.org 
[mailto:ironpython-users-bounces+rome=wintellect....@python.org] On Behalf Of 
Igor Brejc
Sent: Saturday, February 11, 2012 10:11 AM
To: Dino Viehland
Cc: Ironpython-users@python.org
Subject: Re: [Ironpython-users] LightweightScopes and scopes in general

I'm thinking of this scenario: my host application allows users to run their 
own Py scripts. OK, in the ideal case, their scripts should take care of the 
disposal of the reserved resources, but a lot of times user will write 
trail-and-error scripts which will often fail. Is there a mechanism for the 
host application to somehow track (and possibly dispose) of the stuff that has 
been allocated in a failed user script? Using SetTrace() or by simply examining 
the ScriptScope after the script has finished? Or should I simply ignore this?

I'm wondering how other people who use IronPython as an embedded scripting 
engine deal with these issues (if at all).

Thanks,
Igor

On Fri, Feb 10, 2012 at 10:40 PM, Dino Viehland 
<di...@microsoft.com<mailto:di...@microsoft.com>> wrote:
We won’t call IDispose on them.  Usually they’ll follow the pattern of 
implementing IDisposable + having a finalizer so they will eventually release 
their resources.  Also they can be used with the “with” statement so that their 
Dispose methods are eagerly called.

From: Igor Brejc [mailto:igor.br...@gmail.com<mailto:igor.br...@gmail.com>]
Sent: Friday, February 10, 2012 10:51 AM
To: Dino Viehland
Cc: Jeff Hardy; Ironpython-users@python.org<mailto:Ironpython-users@python.org>
Subject: Re: [Ironpython-users] LightweightScopes and scopes in general


On Fri, Feb 10, 2012 at 7:46 PM, Dino Viehland 
<di...@microsoft.com<mailto:di...@microsoft.com>> wrote:


Jeff wrote:
> I'm taking a stab off the top of my head based on my limited knowledge of the
> internals, but I do know that debug code is not collectable.
> It's possible that the code itself may be holding references that keep objects
> alive longer than expected.
>
> I believe if you run the code in a separate AppDomain you can unload the
> AppDomain and the code will be collected, which should take everything else
> with it.
>
> >
> > I've also tried running in the release mode and turning on the
> > options["LightweightScopes"] = true mode, which seems to help. But I
> > cannot find any information about what this option actually does and
> > what happens with the scope variables in general. Any info would be
> appreciated.
>
> That one I'll have to leave to Dino.
The difference between the different types of scopes just comes down to how
we hold onto global variable and call site objects.

The most efficient way to hold onto these is to use CLR static fields to store 
the
global variables.  That allows the CLR to generate a direct access to the field 
and
we can quickly access the global variables.  But it comes at the cost of not 
being
able to recover the static fields and leaking the values that are stored in 
there.

The light weight scopes store the variables in a field or an array instead of 
storing
them in static fields.  To do that we need to pass in the field or close over 
it and then
on each access we need to do the field or array access (and if it's an array 
access, it
needs to be bounds checked).  But the end result is that the only thing which 
is keeping
the global variable / call site objects alive are the delegate which implements 
the
ScriptCode.  Once the ScriptCode goes away all of those call sites and 
PythonGlobal
objects can be collected.

So lightweight scopes come at a performance cost, but they are more applicable
Where you're actually re-compiling code regularly.

Thanks for that explanation, Dino.

Another question that pops up: what happens with Disposable objects (like 
files, GDI+ bitmaps etc.) created within the Py scope (if they are not 
referenced from the outside)?

Igor

_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to