IronPython: Will this leak memory?
I’ve got a massive memory leak in my program. This is the first time I’ve used IronPython in a tight loop, so I’m wondering if this could be the cause.
For Each column In m_Columns
Dim rawValue As String
rawValue = line.Substring(column.FileColumnIndex.Value - 1, column.FileColumnEndIndex.Value - column.FileColumnIndex.Value + 1)
开发者_StackOverflowIf column.IncludeColumnFunction <> "" Then
Dim scope = m_ScriptEngine.CreateScope
scope.SetVariable("Line", line)
scope.SetVariable("Row", targetRow)
If Not CBool(m_ScriptEngine.Execute(column.IncludeColumnFunction, scope)) Then Continue For 'skip this column
End If
targetRow(column.DatabaseColumnName) = column.ParseValue(rawValue, targetRow)
Next
The string named column.IncludeColumnFunction never changes for a given column. It is usually something simple like “Row['Foo'] == 'Bar'”.
Can I/should I be caching the compiled function? Should I be destroying the scope variable in some way when I’m done with it?
Nothing in particular stands out in the code sample that you put up there. I would say it's unlikely that this particular piece of code is causing the issue (although more context is needed to be definitive).
With memory leaks you'll track down the problem much faster by directly investigating the problem vs. digging through code. Direct investigations will often quickly tell you what is leaking and once you know what is leaking then you can start investigating code which manages that type of object.
There are lots of tools and articles out there to help track down memory issues in .Net. Here is a particularly good one.
- http://blogs.msdn.com/b/tess/archive/2008/02/15/net-debugging-demos-lab-3-memory.aspx?wa=wsignin1.0
精彩评论