Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def print_memory(count=30):
'''
Print the statistics of the objects in the memory.
Need pympler to use.
'''
from pympler import muppy, summary
gc.collect()
all_objects = muppy.get_objects()
my_types = muppy.filter(all_objects, Type=wx.Object)
sum1 = summary.summarize(my_types)
# sum1 = summary.summarize(all_objects)
summary.print_(sum1, limit=count)
def memory_usage(where):
"""
Print out a basic summary of memory usage.
Parameters:
where (str):
A string description of where in the code you are summarizing memory usage
"""
assert pympler is not None, 'the pympler python package is required to run this function'
mem_summary = pympler.summary.summarize(pympler.muppy.get_objects())
print("Memory summary: {0}".format(where))
pympler.summary.print_(mem_summary, limit=2)
print("VM: {0:.2f}Mb".format(get_virtual_memory_usage_kb() / 1024.0))
"count": len(all_objects),
"total_size": 0,
"summary": ""
}
sessions = athana._ATHANA_HANDLER.sessions
sessions_info = {
"count": len(sessions),
"total_size": 0,
"summary": ""
}
if pympler:
sessions_info["total_size"] = asizeof(sessions)
summarized_all_objects = sorted(summary.summarize(all_objects), key=lambda t: t[2], reverse=True)
memory_info["summary"] = summarized_all_objects[:500]
import os
if "MEDIATUM_EMBED_IPYTHON" in os.environ:
import IPython
IPython.embed()
del all_objects
return render_template("memstats.j2.jade",
sessions=sessions_info,
memory=memory_info,
naturalsize=humanize.filesize.naturalsize)
% total_time)
tmp_clone_counter = expr_common.clone_counter
if clone_counter != tmp_clone_counter:
clone_counter = tmp_clone_counter
print(" Cloning detected! (clone count: %d)" % clone_counters)
# Note: As is, connectors are expanded when using command-line pyomo but not calling model.create(...) in a Python script.
# John says this has to do with extension points which are called from commandline but not when writing scripts.
# Uncommenting the next two lines switches this (command-line fails because it tries to expand connectors twice)
#connector_expander = ConnectorExpander()
#connector_expander.apply(instance=self)
if (pympler_available is True) and (profile_memory >= 2):
print("")
print(" Summary of objects following instance construction")
post_construction_summary = summary.summarize(muppy.get_objects())
summary.print_(post_construction_summary, limit=100)
print("")
def handle_signal_abort(self, signum, frame):
Log.warn("Someone want to kill me! But I'll not die now! Hahahaha!")
s = summary.summarize(muppy.get_objects())
Log.debug("Current memory usage:")
summary.print_(s)
diff = summary.get_diff(self.mem_sum, s)
self.mem_sum = s
Log.debug("New memory usage:")
summary.print_(diff)
)
from pympler import summary
from pympler import muppy
sum1 = sorted(summary.summarize(muppy.get_objects()), key=lambda r: -r[2])[:30]
Log.warning("{{data}}", data=sum1)
elif end_memory > 1000*1000*1000:
Log.warning(
"MEMORY WARNING (over {{end_memory|comma}}bytes): "+self.description,
default_params=self.params,
end_memory=end_memory
)
from pympler import summary
from pympler import muppy
sum1 = sorted(summary.summarize(muppy.get_objects()), key=lambda r: -r[2])[:30]
Log.warning("{{data}}", data=sum1)
except Exception as e:
Log.warning("problem in memory measure", cause=e)
def format_diff(self, ignore=()):
"""Format the diff to the last time the state of objects was measured.
keyword arguments
ignore -- list of objects to ignore
"""
# ignore this and the caller frame
lines = []
diff = self.get_diff(ignore+(inspect.currentframe(),))
lines.append("Added objects:")
for line in summary.format_(summary.summarize(diff['+'])):
lines.append(line)
lines.append("Removed objects:")
for line in summary.format_(summary.summarize(diff['-'])):
lines.append(line)
return lines