views:

45

answers:

1

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)

     If 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?

A: 

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.

JaredPar
Tools will only tell me what code is leaking today, they won't tell me what will be a problem in the future.
Jonathan Allen
@Jonathan, educating yourself about current problems **will** help you avoid them in the future. Random guessing is just a slower path to this education experience.
JaredPar