hkmoffat / cantera

Automatically exported from code.google.com/p/cantera
0 stars 0 forks source link

Python Memory leak encountered when calling enthalpies_RT or cp_R methods in loop #42

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.  If in Linux, open a terminal and type 'top' and watch the system's memory 
use.
2.  The following code reproduces the problem (You will need to terminate the 
session to stop):
from Cantera import *

R = 8314.0
Temp = 300
Pressure = 101325

gas = importPhase('gri30.cti','gri30')

while 1>0:
    Temp += 1
    Pressure += 30

    gas.set(T=Temp, P=Pressure)
    H2 = gas.enthalpies_RT()*R*Temp   
    Cp2 = gas.cp_R()*R      

What is the expected output? What do you see instead?
Expected Output:
The operating system's memory usage is constant while this executes.

Actual Output:
Viewing the memory usage while running this infinite loop reveals a steady 
increase until a memory allocation error occurs.

What version of the product are you using? On what operating system?
Cantera Version:  1.8.0 beta
Python Version: 2.7
OS: Fedora and Ubuntu 10 (Produced bug on both machines)

Please provide any additional information below.
I suspect this has to do with a pointer being changed before deallocating the 
old memory. I have confirmed this memory leak for both enthalpies_RT() and 
cp_R() methods. 

Also, once the script is terminated, the memory is freed.

The python script to produce this has been attached.

Original issue reported on code.google.com by rocky3...@gmail.com on 18 Apr 2011 at 10:05

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by yarmond on 27 May 2011 at 9:44

GoogleCodeExporter commented 9 years ago
I tested the getEnthalpy_RT() and getPartialMolarEnthalpies() routines in a c++ 
script and didn't produce a memory leak.
This might be happening where python interfaces with the function call.

Original comment by rocky3...@gmail.com on 7 Jul 2011 at 11:40

GoogleCodeExporter commented 9 years ago
I believe I found the fix for this issue.
After commenting out line 201 in ctthermo_methods.cpp located in the source 
distribution under cantera/Cantera/python/src, and reinstalling, the attached 
python script no longer leaked my computer's memory.

The commented line was PyINCREF(x);.
I cannot immediately see increasing the reference count of this object serving 
any purpose since it is returned to python.  Since its returned with a 
reference count of 2, python doesn't garbage collect it, and hence, leaves 
copies of the array in memory.

Original comment by rocky3...@gmail.com on 12 Jul 2011 at 11:16

GoogleCodeExporter commented 9 years ago
Fixed this and a similar issue in "phase_getarray" in r1327.

Original comment by yarmond on 23 Mar 2012 at 10:18