本文整理匯總了Python中gc.garbage方法的典型用法代碼示例。如果您正苦於以下問題:Python gc.garbage方法的具體用法?Python gc.garbage怎麽用?Python gc.garbage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gc
的用法示例。
在下文中一共展示了gc.garbage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_finalizer
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_finalizer(self):
# A() is uncollectable if it is part of a cycle, make sure it shows up
# in gc.garbage.
class A:
def __del__(self): pass
class B:
pass
a = A()
a.a = a
id_a = id(a)
b = B()
b.b = b
gc.collect()
del a
del b
self.assertNotEqual(gc.collect(), 0)
for obj in gc.garbage:
if id(obj) == id_a:
del obj.a
break
else:
self.fail("didn't find obj in garbage (finalizer)")
gc.garbage.remove(obj)
示例2: test_finalizer_newclass
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_finalizer_newclass(self):
# A() is uncollectable if it is part of a cycle, make sure it shows up
# in gc.garbage.
class A(object):
def __del__(self): pass
class B(object):
pass
a = A()
a.a = a
id_a = id(a)
b = B()
b.b = b
gc.collect()
del a
del b
self.assertNotEqual(gc.collect(), 0)
for obj in gc.garbage:
if id(obj) == id_a:
del obj.a
break
else:
self.fail("didn't find obj in garbage (finalizer)")
gc.garbage.remove(obj)
示例3: test_saveall
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_saveall(self):
# Verify that cyclic garbage like lists show up in gc.garbage if the
# SAVEALL option is enabled.
# First make sure we don't save away other stuff that just happens to
# be waiting for collection.
gc.collect()
# if this fails, someone else created immortal trash
self.assertEqual(gc.garbage, [])
L = []
L.append(L)
id_L = id(L)
debug = gc.get_debug()
gc.set_debug(debug | gc.DEBUG_SAVEALL)
del L
gc.collect()
gc.set_debug(debug)
self.assertEqual(len(gc.garbage), 1)
obj = gc.garbage.pop()
self.assertEqual(id(obj), id_L)
示例4: test_boom
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_boom(self):
class Boom:
def __getattr__(self, someattribute):
del self.attr
raise AttributeError
a = Boom()
b = Boom()
a.attr = b
b.attr = a
gc.collect()
garbagelen = len(gc.garbage)
del a, b
# a<->b are in a trash cycle now. Collection will invoke
# Boom.__getattr__ (to see whether a and b have __del__ methods), and
# __getattr__ deletes the internal "attr" attributes as a side effect.
# That causes the trash cycle to get reclaimed via refcounts falling to
# 0, thus mutating the trash graph as a side effect of merely asking
# whether __del__ exists. This used to (before 2.3b1) crash Python.
# Now __getattr__ isn't called.
self.assertEqual(gc.collect(), 4)
self.assertEqual(len(gc.garbage), garbagelen)
示例5: test_boom_new
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_boom_new(self):
# boom__new and boom2_new are exactly like boom and boom2, except use
# new-style classes.
class Boom_New(object):
def __getattr__(self, someattribute):
del self.attr
raise AttributeError
a = Boom_New()
b = Boom_New()
a.attr = b
b.attr = a
gc.collect()
garbagelen = len(gc.garbage)
del a, b
self.assertEqual(gc.collect(), 4)
self.assertEqual(len(gc.garbage), garbagelen)
示例6: test_boom2_new
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_boom2_new(self):
class Boom2_New(object):
def __init__(self):
self.x = 0
def __getattr__(self, someattribute):
self.x += 1
if self.x > 1:
del self.attr
raise AttributeError
a = Boom2_New()
b = Boom2_New()
a.attr = b
b.attr = a
gc.collect()
garbagelen = len(gc.garbage)
del a, b
self.assertEqual(gc.collect(), 4)
self.assertEqual(len(gc.garbage), garbagelen)
示例7: test_main
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_main():
enabled = gc.isenabled()
gc.disable()
assert not gc.isenabled()
debug = gc.get_debug()
gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak
try:
gc.collect() # Delete 2nd generation garbage
run_unittest(GCTests, GCTogglingTests)
finally:
gc.set_debug(debug)
# test gc.enable() even if GC is disabled by default
if verbose:
print "restoring automatic collection"
# make sure to always test gc.enable()
gc.enable()
assert gc.isenabled()
if not enabled:
gc.disable()
示例8: test_doesntBleed
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_doesntBleed(self):
"""
Forcing garbage collection in the test should mean that there are
no unreachable cycles immediately after the test completes.
"""
result = reporter.TestResult()
self.test1(result)
self.flushLoggedErrors() # test1 logs errors that get caught be us.
# test1 created unreachable cycle.
# it & all others should have been collected by now.
if _PY3:
n = len(gc.garbage)
else:
n = gc.collect()
self.assertEqual(n, 0, 'unreachable cycle still existed')
# check that last gc.collect didn't log more errors
x = self.flushLoggedErrors()
self.assertEqual(len(x), 0, 'Errors logged after gc.collect')
示例9: GClector
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def GClector(self):
print('GC is invoked, %d calls in map' % len(self.ccmap))
if self.debug_mode:
print(self.global_config['_sip_tm'].tclient, self.global_config['_sip_tm'].tserver)
for cc in tuple(self.ccmap):
try:
print(cc.uaA.state, cc.uaO.state)
except AttributeError:
print(None)
else:
print('[%d]: %d client, %d server transactions in memory' % \
(os.getpid(), len(self.global_config['_sip_tm'].tclient), len(self.global_config['_sip_tm'].tserver)))
if self.safe_restart:
if len(self.ccmap) == 0:
self.global_config['_sip_tm'].userv.close()
os.chdir(self.global_config['_orig_cwd'])
argv = [sys.executable,]
argv.extend(self.global_config['_orig_argv'])
os.execv(sys.executable, argv)
# Should not reach this point!
self.el.ival = 1
#print gc.collect()
if len(gc.garbage) > 0:
print(gc.garbage)
示例10: cleanup_traceback
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def cleanup_traceback(self):
"""Remove local variables reference from tracebacks to allow garbage
collector to clean all Qubes*() objects, otherwise file descriptors
held by them will leak"""
exc_infos = [e for test_case, e in self._outcome.errors
if test_case is self]
if self._outcome.expectedFailure:
exc_infos.append(self._outcome.expectedFailure)
for exc_info in exc_infos:
if exc_info is None:
continue
ex = exc_info[1]
while ex is not None:
if isinstance(ex, qubes.exc.QubesVMError):
ex.vm = None
traceback.clear_frames(ex.__traceback__)
ex = ex.__context__
示例11: cleanup_gc
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [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
示例12: test_it
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_it(collectible):
dd()
dd('======= ', ('collectible' if collectible else 'uncollectible'), ' object =======')
dd()
gc.collect()
dd('*** init, nr of referrers: ', len(gc.get_referrers(One)))
dd(' garbage: ', gc.garbage)
one = One(collectible)
dd(' created: ', one.typ, ': ', one)
dd(' nr of referrers: ', len(gc.get_referrers(One)))
dd(' delete:')
del one
gc.collect()
dd('*** after gc, nr of referrers: ', len(gc.get_referrers(One)))
dd(' garbage: ', gc.garbage)
示例13: clear
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def clear(self):
if self._eqcache:
eqc = self._eqcache
self._eqcache = {}
while eqc:
name, obj = eqc.popitem()
try:
obj.clear()
except:
logging.exception_error("environment clear: {!r}".format(obj))
gc.collect()
for obj in gc.garbage:
try:
obj.close()
except:
logging.exception_warning("environment garbage collect: {!r}".format(obj))
del gc.garbage[:]
scheduler.sleep(2) # some devices need time to fully clear or disconnect
示例14: test_garbageCollectedTransactionAborts
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def test_garbageCollectedTransactionAborts(self):
"""
When an L{IAsyncTransaction} is garbage collected, it ought to abort
itself.
"""
t = self.createTransaction()
self.resultOf(t.execSQL("echo", []))
conns = self.factory.connections
self.assertEquals(len(conns), 1)
self.assertEquals(conns[0]._rollbackCount, 0)
del t
gc.collect()
self.flushHolders()
self.assertEquals(len(conns), 1)
self.assertEquals(conns[0]._rollbackCount, 1)
self.assertEquals(conns[0]._commitCount, 0)
示例15: countObjects
# 需要導入模塊: import gc [as 別名]
# 或者: from gc import garbage [as 別名]
def countObjects(self, ao):
"""
Recursively find non-list,dict, tuple objects in containers.
Essential for traversing the garbage collector
"""
itemType = type(ao)
counter = self[itemType]
if counter.add(ao):
self.count += 1
if self.count % 100000 == 0:
runLog.info("Counted {} items".format(self.count))
if isinstance(ao, dict):
for k, v in ao.items():
self.countObjects(k)
self.countObjects(v)
elif isinstance(ao, (list, tuple, set)):
for v in iter(ao):
self.countObjects(v)