Topics

ScriptScope

Hailiang Shen
 

Dear all, this is my first post so please kindly notify if it violates any rules here.

 

I am hosting IronPython in a C# program. My question is on using class ScriptScope. Will all python variables in the script removed from memory after run? I am worrying they will stay in memory and eventually blow up.

 

If I use scope.GetVariable() to pull a python variable (can be a basic python data type, .NET type, or my python class) into C#, I can still access the pulled variable after the scope is done. How IronPython is handing this case, does it actually keep the instance of the pulled variable in memory and remove all other variables that are not pulled?

 

Thanks

 

 

Hailiang Shen, Ph.D., P.Eng.

Computational Hydraulics Int. (CHI)

519.767.0197 ext. 1015 | hailiang@... | www.chiwater.com

 

We invite you to join us at the 2019 ICWMM on Feb 27 & 28 in Toronto, Canada

 

Alex Earl
 

Hello Hailiang,

The variables will be around for as long as the ScriptScope is around. As C# doesn't have deterministic garbage collection, the ScriptScope may be around for some time. 

Hope that helps,

Alex

On Wed, Jan 30, 2019 at 1:48 PM Hailiang Shen via Groups.Io <hailiang=chiwater.com@groups.io> wrote:

Dear all, this is my first post so please kindly notify if it violates any rules here.

 

I am hosting IronPython in a C# program. My question is on using class ScriptScope. Will all python variables in the script removed from memory after run? I am worrying they will stay in memory and eventually blow up.

 

If I use scope.GetVariable() to pull a python variable (can be a basic python data type, .NET type, or my python class) into C#, I can still access the pulled variable after the scope is done. How IronPython is handing this case, does it actually keep the instance of the pulled variable in memory and remove all other variables that are not pulled?

 

Thanks

 

 

Hailiang Shen, Ph.D., P.Eng.

Computational Hydraulics Int. (CHI)

519.767.0197 ext. 1015 | hailiang@... | www.chiwater.com

 

We invite you to join us at the 2019 ICWMM on Feb 27 & 28 in Toronto, Canada

 



Hailiang Shen
 

Thanks Alex. If I have 10 variables in a scope, which is defined in a run method (see below). If I use scope.GetVariable to pull the first variable, will the remaining 9 variables handled by .NET garbage collector, or will the 10 scope variables be hold off until the first variable is dereferenced?

 

void Run(string scriptName, TextWriter txtOutput = null)

        {

            ScriptEngine m_engine = null;

            ScriptSource m_source;

            ScriptScope m_scope;

            try

            {

                this.console = txtOutput;

                m_engine = getScriptEngine(txtOutput);

                m_source = m_engine.CreateScriptSourceFromFile(ScriptBase.GetScriptPath(scriptName));

                m_source.Compile();

 

                m_scope = m_engine.CreateScope();

                m_scope.SetVariable(variable, this);   

 

                m_source.Execute(m_scope);

            }

            catch (Exception err)

            {

               

            }

        }

 

Hailiang

 

From: users@ironpython.groups.io <users@ironpython.groups.io> On Behalf Of Alex Earl
Sent: January 30, 2019 4:05 PM
To: users@ironpython.groups.io
Subject: Re: [ironpython] ScriptScope

 

Hello Hailiang,

 

The variables will be around for as long as the ScriptScope is around. As C# doesn't have deterministic garbage collection, the ScriptScope may be around for some time. 

 

Hope that helps,

 

Alex

 

On Wed, Jan 30, 2019 at 1:48 PM Hailiang Shen via Groups.Io <hailiang=chiwater.com@groups.io> wrote:

Dear all, this is my first post so please kindly notify if it violates any rules here.

 

I am hosting IronPython in a C# program. My question is on using class ScriptScope. Will all python variables in the script removed from memory after run? I am worrying they will stay in memory and eventually blow up.

 

If I use scope.GetVariable() to pull a python variable (can be a basic python data type, .NET type, or my python class) into C#, I can still access the pulled variable after the scope is done. How IronPython is handing this case, does it actually keep the instance of the pulled variable in memory and remove all other variables that are not pulled?

 

Thanks

 

 

Hailiang Shen, Ph.D., P.Eng.

Computational Hydraulics Int. (CHI)

519.767.0197 ext. 1015 | hailiang@... | www.chiwater.com

 

We invite you to join us at the 2019 ICWMM on Feb 27 & 28 in Toronto, Canada

 


 

--

Alex Earl
 

It looks like you are creating a new engine each time you run something, so GC should collect previous runs if that is what you are asking.


On Thu, Jan 31, 2019 at 6:32 AM Hailiang Shen via Groups.Io <hailiang=chiwater.com@groups.io> wrote:

Thanks Alex. If I have 10 variables in a scope, which is defined in a run method (see below). If I use scope.GetVariable to pull the first variable, will the remaining 9 variables handled by .NET garbage collector, or will the 10 scope variables be hold off until the first variable is dereferenced?

 

void Run(string scriptName, TextWriter txtOutput = null)

        {

            ScriptEngine m_engine = null;

            ScriptSource m_source;

            ScriptScope m_scope;

            try

            {

                this.console = txtOutput;

                m_engine = getScriptEngine(txtOutput);

                m_source = m_engine.CreateScriptSourceFromFile(ScriptBase.GetScriptPath(scriptName));

                m_source.Compile();

 

                m_scope = m_engine.CreateScope();

                m_scope.SetVariable(variable, this);   

 

                m_source.Execute(m_scope);

            }

            catch (Exception err)

            {

               

            }

        }

 

Hailiang

 

From: users@ironpython.groups.io <users@ironpython.groups.io> On Behalf Of Alex Earl
Sent: January 30, 2019 4:05 PM
To: users@ironpython.groups.io
Subject: Re: [ironpython] ScriptScope

 

Hello Hailiang,

 

The variables will be around for as long as the ScriptScope is around. As C# doesn't have deterministic garbage collection, the ScriptScope may be around for some time. 

 

Hope that helps,

 

Alex

 

On Wed, Jan 30, 2019 at 1:48 PM Hailiang Shen via Groups.Io <hailiang=chiwater.com@groups.io> wrote:

Dear all, this is my first post so please kindly notify if it violates any rules here.

 

I am hosting IronPython in a C# program. My question is on using class ScriptScope. Will all python variables in the script removed from memory after run? I am worrying they will stay in memory and eventually blow up.

 

If I use scope.GetVariable() to pull a python variable (can be a basic python data type, .NET type, or my python class) into C#, I can still access the pulled variable after the scope is done. How IronPython is handing this case, does it actually keep the instance of the pulled variable in memory and remove all other variables that are not pulled?

 

Thanks

 

 

Hailiang Shen, Ph.D., P.Eng.

Computational Hydraulics Int. (CHI)

519.767.0197 ext. 1015 | hailiang@... | www.chiwater.com

 

We invite you to join us at the 2019 ICWMM on Feb 27 & 28 in Toronto, Canada

 


 

--



László Frank
 

Alex, 

exactly because what you just mentnioned, I am also wondering why ScriptScope or even PythonEngine do not implement IDisposable ?

That way one would have control.

Laszlo

On Wed, Jan 30, 2019, 22:05 Alex Earl <slide.o.mix@...> wrote:
Hello Hailiang,

The variables will be around for as long as the ScriptScope is around. As C# doesn't have deterministic garbage collection, the ScriptScope may be around for some time. 

Hope that helps,

Alex

On Wed, Jan 30, 2019 at 1:48 PM Hailiang Shen via Groups.Io <hailiang=chiwater.com@groups.io> wrote:

Dear all, this is my first post so please kindly notify if it violates any rules here.

 

I am hosting IronPython in a C# program. My question is on using class ScriptScope. Will all python variables in the script removed from memory after run? I am worrying they will stay in memory and eventually blow up.

 

If I use scope.GetVariable() to pull a python variable (can be a basic python data type, .NET type, or my python class) into C#, I can still access the pulled variable after the scope is done. How IronPython is handing this case, does it actually keep the instance of the pulled variable in memory and remove all other variables that are not pulled?

 

Thanks

 

 

Hailiang Shen, Ph.D., P.Eng.

Computational Hydraulics Int. (CHI)

519.767.0197 ext. 1015 | hailiang@... | www.chiwater.com

 

We invite you to join us at the 2019 ICWMM on Feb 27 & 28 in Toronto, Canada

 



--

Stéphane Lozier
 

I think you misunderstand what IDisposable is used for, it is to release unmanaged resources (like database connections, file handles, etc.). Just because you call Dispose on something it doesn't mean the GC will free up the memory.

Stéphane

On Fri, Feb 1, 2019, 1:30 AM László Frank <laszlo.frank@... wrote:
Alex, 

exactly because what you just mentnioned, I am also wondering why ScriptScope or even PythonEngine do not implement IDisposable ?

That way one would have control.

Laszlo

On Wed, Jan 30, 2019, 22:05 Alex Earl <slide.o.mix@...> wrote:
Hello Hailiang,

The variables will be around for as long as the ScriptScope is around. As C# doesn't have deterministic garbage collection, the ScriptScope may be around for some time. 

Hope that helps,

Alex

On Wed, Jan 30, 2019 at 1:48 PM Hailiang Shen via Groups.Io <hailiang=chiwater.com@groups.io> wrote:

Dear all, this is my first post so please kindly notify if it violates any rules here.

 

I am hosting IronPython in a C# program. My question is on using class ScriptScope. Will all python variables in the script removed from memory after run? I am worrying they will stay in memory and eventually blow up.

 

If I use scope.GetVariable() to pull a python variable (can be a basic python data type, .NET type, or my python class) into C#, I can still access the pulled variable after the scope is done. How IronPython is handing this case, does it actually keep the instance of the pulled variable in memory and remove all other variables that are not pulled?

 

Thanks

 

 

Hailiang Shen, Ph.D., P.Eng.

Computational Hydraulics Int. (CHI)

519.767.0197 ext. 1015 | hailiang@... | www.chiwater.com

 

We invite you to join us at the 2019 ICWMM on Feb 27 & 28 in Toronto, Canada

 



--

László Frank
 

Thanks Stéphane, I understand that calling Dispose() itself won't free up memory, but I believe Dispose may release specific internal references which can help GC.
On the other hand, I am not sure if ScriptScope or PythonEngine is not using any unmanaged - hence disposable - resources.
Of course, I can also ensure my code is correctly releasing the reference to ScriptScope for instance.

On Fri, Feb 1, 2019 at 2:34 PM Stéphane Lozier <stephane.lozier@...> wrote:
I think you misunderstand what IDisposable is used for, it is to release unmanaged resources (like database connections, file handles, etc.). Just because you call Dispose on something it doesn't mean the GC will free up the memory.

Stéphane

On Fri, Feb 1, 2019, 1:30 AM László Frank <laszlo.frank@... wrote:
Alex, 

exactly because what you just mentnioned, I am also wondering why ScriptScope or even PythonEngine do not implement IDisposable ?

That way one would have control.

Laszlo

On Wed, Jan 30, 2019, 22:05 Alex Earl <slide.o.mix@...> wrote:
Hello Hailiang,

The variables will be around for as long as the ScriptScope is around. As C# doesn't have deterministic garbage collection, the ScriptScope may be around for some time. 

Hope that helps,

Alex

On Wed, Jan 30, 2019 at 1:48 PM Hailiang Shen via Groups.Io <hailiang=chiwater.com@groups.io> wrote:

Dear all, this is my first post so please kindly notify if it violates any rules here.

 

I am hosting IronPython in a C# program. My question is on using class ScriptScope. Will all python variables in the script removed from memory after run? I am worrying they will stay in memory and eventually blow up.

 

If I use scope.GetVariable() to pull a python variable (can be a basic python data type, .NET type, or my python class) into C#, I can still access the pulled variable after the scope is done. How IronPython is handing this case, does it actually keep the instance of the pulled variable in memory and remove all other variables that are not pulled?

 

Thanks

 

 

Hailiang Shen, Ph.D., P.Eng.

Computational Hydraulics Int. (CHI)

519.767.0197 ext. 1015 | hailiang@... | www.chiwater.com

 

We invite you to join us at the 2019 ICWMM on Feb 27 & 28 in Toronto, Canada

 



--