本文整理汇总了Python中gc.get_objects方法的典型用法代码示例。如果您正苦于以下问题:Python gc.get_objects方法的具体用法?Python gc.get_objects怎么用?Python gc.get_objects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gc
的用法示例。
在下文中一共展示了gc.get_objects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: count
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def count(typename, objects=None):
"""Count objects tracked by the garbage collector with a given class name.
Example:
>>> count('dict')
42
>>> count('MyClass', get_leaking_objects())
3
Note that the GC does not track simple objects like int or str.
.. versionchanged:: 1.7
New parameter: ``objects``.
"""
if objects is None:
objects = gc.get_objects()
return sum(1 for o in objects if type(o).__name__ == typename)
示例2: get_leaking_objects
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def get_leaking_objects(objects=None):
"""Return objects that do not have any referents.
These could indicate reference-counting bugs in C code. Or they could
be legitimate.
Note that the GC does not track simple objects like int or str.
.. versionadded:: 1.7
"""
if objects is None:
gc.collect()
objects = gc.get_objects()
try:
ids = set(id(i) for i in objects)
for i in objects:
ids.difference_update(id(j) for j in gc.get_referents(i))
# this then is our set of objects without referrers
return [i for i in objects if id(i) in ids]
finally:
objects = i = j = None # clear cyclic references to frame
示例3: by_type
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def by_type(typename, objects=None):
"""Return objects tracked by the garbage collector with a given class name.
Example:
>>> by_type('MyClass')
[<mymodule.MyClass object at 0x...>]
Note that the GC does not track simple objects like int or str.
.. versionchanged:: 1.7
New parameter: ``objects``.
"""
if objects is None:
objects = gc.get_objects()
return [o for o in objects if type(o).__name__ == typename]
示例4: memory_used
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def memory_used():
result = {}
for obj in gc.get_objects():
try:
if torch.is_tensor(obj):
if type(obj) not in result:
result[type(obj)] = 0
count = 1
for elem in list(obj.data.size()):
count *= elem
result[type(obj.data)] += count * obj.data.element_size()
elif hasattr(obj, 'data') and torch.is_tensor(obj.data):
if type(obj.data) not in result:
result[type(obj.data)] = 0
count = 1
for elem in list(obj.data.size()):
count *= elem
result[type(obj.data)] += count * obj.data.element_size()
except:
print("could not track ...")
return result
示例5: cleanup_gc
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def cleanup_gc(self):
gc.collect()
leaked = [obj for obj in gc.get_objects() + gc.garbage
if isinstance(obj,
(qubes.Qubes, qubes.vm.BaseVM,
libvirt.virConnect, libvirt.virDomain))]
if leaked:
try:
import objgraph
objgraph.show_backrefs(leaked,
max_depth=15, extra_info=extra_info,
filename='/tmp/objgraph-{}.png'.format(
self.id()))
except ImportError:
pass
# do not keep leaked object references in locals()
leaked = bool(leaked)
assert not leaked
示例6: host_memory_usage_in_gb
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def host_memory_usage_in_gb():
gc.collect()
gc.disable() # Avoids accessing gc'd objects during traversal.
objects = gc.get_objects()
tensors = [obj for obj in objects if torch.is_tensor(obj)] # Debug
host_tensors = [t for t in tensors if not t.is_cuda]
total_mem_mb = 0
visited_data = []
for tensor in host_tensors:
if tensor.is_sparse:
continue
# a data_ptr indicates a memory block allocated
data_ptr = tensor.storage().data_ptr()
if data_ptr in visited_data:
continue
visited_data.append(data_ptr)
numel = tensor.storage().size()
element_size = tensor.storage().element_size()
mem_mb = numel*element_size /1024/1024 # 32bit=4Byte, MByte
total_mem_mb += mem_mb
gc.enable()
return total_mem_mb / 1024 # in
示例7: test_teardown_issue1649
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def test_teardown_issue1649(testdir):
"""
Are TestCase objects cleaned up? Often unittest TestCase objects set
attributes that are large and expensive during setUp.
The TestCase will not be cleaned up if the test fails, because it
would then exist in the stackframe.
"""
testpath = testdir.makepyfile(
"""
import unittest
class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase):
def setUp(self):
self.an_expensive_object = 1
def test_demo(self):
pass
"""
)
testdir.inline_run("-s", testpath)
gc.collect()
for obj in gc.get_objects():
assert type(obj).__name__ != "TestCaseObjectsShouldBeCleanedUp"
示例8: disconnectAllHdfDBs
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def disconnectAllHdfDBs():
"""
Forcibly disconnect all instances of HdfDB objects
Notes
-----
This is a hack to help ARMI exit gracefully when the garbage collector and h5py have
issues destroying objects. After lots of investigation, the root cause for why this
was having issues was never identified. It appears that when several HDF5 files are
open in the same run (e.g. when calling armi.init() multiple times from a
post-processing script), when these h5py File objects were closed, the garbage
collector would raise an exception related to the repr'ing the object. We
get around this by using the garbage collector to manually disconnect all open HdfDB
objects.
"""
h5dbs = [db for db in gc.get_objects() if isinstance(db, Database3)]
for db in h5dbs:
db.close()
示例9: observation_containers
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def observation_containers(self):
# Return the current set of 'observation containers'
# as discussed in Notes Oct 27 2005.
# returns a nodeset, not an idset, to avoid recursive referenes
objs = self.gc.get_objects()
cli = self.hv.cli_type()
objs = (cli.select(objs, self.NodeSet, '<=') +
cli.select(objs, ObservationList, '<=') +
cli.select(
objs, self._parent.UniSet.IdentitySetSingleton, '<=')
)
r = self.immnodeset([x for x in objs if getattr(
x, '_hiding_tag_', None) is self._hiding_tag_])
del cli, objs
return r
示例10: obj_referrers
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def obj_referrers(klass):
find_obj = False
for obj in gc.get_objects():
# closures are evil !
if isinstance(obj, types.FunctionType) and obj.__closure__ is not None:
for c in obj.__closure__:
try:
if isinstance(c.cell_contents, klass):
print('!!!', obj, c.cell_contents)
except ValueError:
print("Cell is empty...")
if isinstance(obj, klass):
find_obj = True
rs = gc.get_referrers(obj)
print("---------------------------referrers of %s" % klass.__name__)
for ob in rs:
print(type(ob), ob.__name__ if type(ob) is type else repr(ob)[:140])
rs1 = gc.get_referrers(ob)
for ob1 in rs1:
print(' ', type(ob1), ob1.__name__ if type(ob1) is type else repr(ob1)[:140])
print("---------------------------")
if not find_obj:
print("Nothing refrences %s" % klass.__name__)
示例11: print_muppy_sumary
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def print_muppy_sumary():
# http://pythonhosted.org/Pympler/index.html
try:
from pympler import muppy, summary
except ImportError:
print("WARNING: pympler not installed")
return
# from pympler.classtracker import ClassTracker
# from pympler.classtracker_stats import HtmlStats
global all_objects, obj_summary, class_tracker
if all_objects is None:
all_objects = muppy.get_objects()
obj_summary = summary.summarize(all_objects)
summary.print_(obj_summary)
# class_tracker = ClassTracker()
# class_tracker.track_class(FICSPlayer, trace=1)
# class_tracker.track_class(ICGameModel, resolution_level=2, trace=1)
else:
obj_summary2 = summary.summarize(muppy.get_objects())
diff = summary.get_diff(obj_summary, obj_summary2)
summary.print_(diff, limit=200)
# class_tracker.create_snapshot('usage')
# HtmlStats(tracker=class_tracker).create_html('profile.html')
示例12: get_instances
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def get_instances(cls):
return [x for x in gc.get_objects() if isinstance(x, cls)]
示例13: count
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def count(typename):
"""Count objects tracked by the garbage collector with a given class name.
Example:
>>> count('dict')
42
>>> count('MyClass')
3
Note that the GC does not track simple objects like int or str.
"""
return sum(1 for o in gc.get_objects() if type(o).__name__ == typename)
示例14: typestats
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def typestats():
"""Count the number of instances for each type tracked by the GC.
Note that the GC does not track simple objects like int or str.
Note that classes with the same name but defined in different modules
will be lumped together.
"""
stats = {}
for o in gc.get_objects():
stats.setdefault(type(o).__name__, 0)
stats[type(o).__name__] += 1
return stats
示例15: by_type
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_objects [as 别名]
def by_type(typename):
"""Return objects tracked by the garbage collector with a given class name.
Example:
>>> by_type('MyClass')
[<mymodule.MyClass object at 0x...>]
Note that the GC does not track simple objects like int or str.
"""
return [o for o in gc.get_objects() if type(o).__name__ == typename]