Skip to content

Instantly share code, notes, and snippets.

@adamgreenhall
Created October 8, 2012 21:35
Show Gist options
  • Save adamgreenhall/3855150 to your computer and use it in GitHub Desktop.
Save adamgreenhall/3855150 to your computer and use it in GitHub Desktop.
memory leaks in coopr sets
import objgraph
class BaseClass(object):
def some_method(args): print args
class DerivedClass(BaseClass):
def __init__(self, *args):
print 'new derived class'
self.args = args
x = DerivedClass()
objgraph.show_refs(x, filename='test.png')
set ITEMS := hammer wrench screwdriver towel ;
param : v w :=
hammer 8 5
wrench 3 7
screwdriver 6 4
towel 11 3;
param limit := 14;
from coopr.pyomo import Var, Set, Constraint, Objective, Param, AbstractModel, PositiveReals, Binary, maximize
model = AbstractModel()
model.ITEMS = Set()
model.v = Param(model.ITEMS, within=PositiveReals)
model.w = Param(model.ITEMS, within=PositiveReals)
model.limit = Param(within=PositiveReals)
model.x = Var(model.ITEMS, within=Binary)
def value_rule(model):
return sum(model.v[i]*model.x[i] for i in model.ITEMS)
model.value = Objective(sense=maximize)
def weight_rule(model):
return sum(model.w[i]*model.x[i] for i in model.ITEMS) <= model.limit
model.weight = Constraint()
sudo apt-get install glpk
pip install --use-mirrors objgraph coopr
import objgraph
import inspect
import gc
from pprint import pprint
from pdb import set_trace as debug
test_types = [
'Var',
'Piecewise',
'ScenarioTree',
'ScenarioTreeNode',
'Scenario',
'_SetContainer',
'_ConstraintArray',
]
def test_memory():
# use objgraph to check if these pyomo objects still exist
test_counts = {}
for name in test_types:
objects = objgraph.by_type( name )
test_counts[name] = len(objects)
if test_counts[name]==0: continue
else:
obj = objects[0]
objects = []
# debug()
# pprint(inspect.getmembers(obj))
fname = 'objgraph-{}-'.format(name)
objgraph.show_refs( [obj], filename=fname+'refs.png', extra_info=lambda x: id(x)) # too_many=50,
objgraph.show_refs( [obj.__class__], filename=fname+'class-refs.png', extra_info=lambda x: id(x), max_depth=2) # too_many=50,
# print filter(lambda m: type(getattr(obj.__class__, m[0]))==tuple, inspect.getmembers(obj.__class__))
objgraph.show_backrefs([obj], filename=fname+'backref.png')
objgraph.show_chain(
objgraph.find_backref_chain( obj, inspect.ismodule),
filename=fname+'chain.png'
)
# gc.collect()
# print 'referrers'
# pprint(gc.get_referrers(obj))
return test_counts
def run():
# Import model
import coopr.opt
import knapsack
from coopr import pyomo
# Create the model instance
instance = knapsack.model.create("knapsack.dat")
#
# Setup the optimizer
opt = coopr.opt.SolverFactory("glpk")
#
# Optimize
results = opt.solve(instance)
#
# Write the output
# results.write(num=1)
#knapsack.model.ITEMS.__dict__ = {}
#knapsack.model.ITEMS.name = 'ITEMS'
# obj = objgraph.by_type( '_SetContainer' )[0]
# objgraph.show_backrefs([obj], filename='_SetContainer-inline-backref.png')
# debug()
# try and fix the gc problems
# knapsack.model.ITEMS.__dict__ = {}
# knapsack.model.ITEMS.name = 'ITEMS'
for component in knapsack.model.active_components(pyomo.Var).values():
component._index = None
component._data = None
for component in knapsack.model.active_components(pyomo.Param).values():
component._index = None
component._data = None
knapsack.model.components = None
# for component in knapsack.model.active_components(pyomo.Block).values():
# component.ITEMS = None
# component.__dict__._declarations = None
# component.__dict__._component = None
# knapsack.model.__dict__ = {}
# knapsack.model.name = 'knapsack'
#
run()
test_counts = test_memory()
uncollectable_count = gc.collect()
# print 'garbage'
# print gc.garbage
# print 'Garbage'
# pprint( [item for item in gc.garbage] )
print 'garbage count', uncollectable_count
assert sum(test_counts.values()) == 0
assert uncollectable_count==0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment