本文整理匯總了Python中sys.getallocatedblocks方法的典型用法代碼示例。如果您正苦於以下問題:Python sys.getallocatedblocks方法的具體用法?Python sys.getallocatedblocks怎麽用?Python sys.getallocatedblocks使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sys
的用法示例。
在下文中一共展示了sys.getallocatedblocks方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _refleak_cleanup
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getallocatedblocks [as 別名]
def _refleak_cleanup():
# Collect cyclic trash and read memory statistics immediately after.
try:
func1 = sys.getallocatedblocks
except AttributeError:
def func1():
return 42
try:
func2 = sys.gettotalrefcount
except AttributeError:
def func2():
return 42
# Flush standard output, so that buffered data is sent to the OS and
# associated Python objects are reclaimed.
for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
if stream is not None:
stream.flush()
sys._clear_type_cache()
# This also clears the various internal CPython freelists.
gc.collect()
return func1(), func2()
示例2: _run_test
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getallocatedblocks [as 別名]
def _run_test(self, work_func: FunctionType, work_resource: object,
jobs: int, trials: int,
show_progress: bool=False) -> Mapping:
results = {
'jobs': jobs,
'trials': trials,
'time': [],
'blocks': [],
}
# Forcibly evaluate the inputs to prevent time/resources taken up later
inputs = list(zip(
[work_resource] * jobs,
range(jobs)
))
trial_iter = range(trials)
if show_progress is True and trials > 2:
trial_iter = tqdm(trial_iter, desc='trials')
gc.collect()
for _ in trial_iter:
# Run trial of pool map function and measure it
gc.collect()
blocks_start = sys.getallocatedblocks()
time_start = time.time()
list(self.map(work_func, inputs))
time_end = time.time()
results['time'].append(time_end - time_start)
# Get allocated blocks before garbage collection to show peak usage
blocks_end = sys.getallocatedblocks()
results['blocks'].append(blocks_end - blocks_start)
return results
示例3: test_getallocatedblocks
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getallocatedblocks [as 別名]
def test_getallocatedblocks(self):
# Some sanity checks
with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC')
a = sys.getallocatedblocks()
self.assertIs(type(a), int)
if with_pymalloc:
self.assertGreater(a, 0)
else:
# When WITH_PYMALLOC isn't available, we don't know anything
# about the underlying implementation: the function might
# return 0 or something greater.
self.assertGreaterEqual(a, 0)
try:
# While we could imagine a Python session where the number of
# multiple buffer objects would exceed the sharing of references,
# it is unlikely to happen in a normal test run.
self.assertLess(a, sys.gettotalrefcount())
except AttributeError:
# gettotalrefcount() not available
pass
gc.collect()
b = sys.getallocatedblocks()
self.assertLessEqual(b, a)
gc.collect()
c = sys.getallocatedblocks()
self.assertIn(c, range(b - 50, b + 50))
示例4: dash_R_cleanup
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getallocatedblocks [as 別名]
def dash_R_cleanup(fs, ps, pic, zdc, abcs):
import gc, copyreg
import collections.abc
# Restore some original values.
warnings.filters[:] = fs
copyreg.dispatch_table.clear()
copyreg.dispatch_table.update(ps)
sys.path_importer_cache.clear()
sys.path_importer_cache.update(pic)
try:
import zipimport
except ImportError:
pass # Run unmodified on platforms without zipimport support
else:
zipimport._zip_directory_cache.clear()
zipimport._zip_directory_cache.update(zdc)
# clear type cache
sys._clear_type_cache()
# Clear ABC registries, restoring previously saved ABC registries.
abs_classes = [getattr(collections.abc, a) for a in collections.abc.__all__]
abs_classes = filter(isabstract, abs_classes)
for abc in abs_classes:
for obj in abc.__subclasses__() + [abc]:
for ref in abcs.get(obj, set()):
if ref() is not None:
obj.register(ref())
obj._abc_caches_clear()
clear_caches()
# Collect cyclic trash and read memory statistics immediately after.
func1 = sys.getallocatedblocks
func2 = sys.gettotalrefcount
gc.collect()
return func1(), func2(), support.fd_count()
示例5: _huntLeaks
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getallocatedblocks [as 別名]
def _huntLeaks(self, test):
self.stream.flush()
repcount = self.repetitions
nwarmup = self.warmup
rc_deltas = [0] * (repcount - nwarmup)
alloc_deltas = [0] * (repcount - nwarmup)
# Preallocate ints likely to be stored in rc_deltas and alloc_deltas,
# to make sys.getallocatedblocks() less flaky.
_int_pool = IntPool()
for i in range(-200, 200):
_int_pool[i]
alloc_before = rc_before = 0
for i in range(repcount):
# Use a pristine, silent result object to avoid recursion
res = result.TestResult()
test.run(res)
# Poorly-written tests may fail when run several times.
# In this case, abort the refleak run and report the failure.
if not res.wasSuccessful():
self.failures.extend(res.failures)
self.errors.extend(res.errors)
raise AssertionError
del res
alloc_after, rc_after = _refleak_cleanup()
if i >= nwarmup:
rc_deltas[i - nwarmup] = _int_pool[rc_after - rc_before]
alloc_deltas[i -
nwarmup] = _int_pool[alloc_after -
alloc_before]
alloc_before, rc_before = alloc_after, rc_after
return rc_deltas, alloc_deltas
示例6: dash_R_cleanup
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import getallocatedblocks [as 別名]
def dash_R_cleanup(fs, ps, pic, zdc, abcs):
import gc, copyreg
import collections.abc
from weakref import WeakSet
# Restore some original values.
warnings.filters[:] = fs
copyreg.dispatch_table.clear()
copyreg.dispatch_table.update(ps)
sys.path_importer_cache.clear()
sys.path_importer_cache.update(pic)
try:
import zipimport
except ImportError:
pass # Run unmodified on platforms without zipimport support
else:
zipimport._zip_directory_cache.clear()
zipimport._zip_directory_cache.update(zdc)
# clear type cache
sys._clear_type_cache()
# Clear ABC registries, restoring previously saved ABC registries.
abs_classes = [getattr(collections.abc, a) for a in collections.abc.__all__]
abs_classes = filter(isabstract, abs_classes)
if 'typing' in sys.modules:
t = sys.modules['typing']
# these classes require special treatment because they do not appear
# in direct subclasses on collections.abc classes
abs_classes = list(abs_classes) + [t.ChainMap, t.Counter, t.DefaultDict]
for abc in abs_classes:
for obj in abc.__subclasses__() + [abc]:
obj._abc_registry = abcs.get(obj, WeakSet()).copy()
obj._abc_cache.clear()
obj._abc_negative_cache.clear()
clear_caches()
# Collect cyclic trash and read memory statistics immediately after.
func1 = sys.getallocatedblocks
func2 = sys.gettotalrefcount
gc.collect()
return func1(), func2()