def test_destructor(): class Hello(object): deleted = False def say(self): pass def __del__(self): Hello.deleted = True with JSContext() as ctxt: fn = ctxt.eval("(function (obj) { obj.say(); })") obj = Hello() assert 2 == sys.getrefcount(obj) fn(obj) assert 4 == sys.getrefcount(obj) assert not Hello.deleted del fn del ctxt # causes segfault # JSEngine.collect() assert 2 == sys.getrefcount(obj) del obj assert Hello.deleted
def test_bug_782369(self): for i in range(10): b = array.array('B', range(64)) rc = sys.getrefcount(10) for i in range(10): b = array.array('B', range(64)) self.assertEqual(rc, sys.getrefcount(10))
def check_set_complex(self,level=5): a = UserDict() key = 1+1j inline_tools.inline("a[key] = 1234;",['a','key']) assert_equal(sys.getrefcount(key),3) assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],1234)
def test04_leak_GC(self): import sys a = "example of private object" refcount = sys.getrefcount(a) self.obj.set_private(a) self.obj = None self.assertEqual(refcount, sys.getrefcount(a))
def test_no_refcycle_through_target(self): class RunSelfFunction(object): def __init__(self, should_raise): # The links in this refcycle from Thread back to self # should be cleaned up when the thread completes. self.should_raise = should_raise self.thread = threading.Thread(target=self._run, args=(self,), kwargs={"yet_another": self}) self.thread.start() def _run(self, other_ref, yet_another): if self.should_raise: raise SystemExit cyclic_object = RunSelfFunction(should_raise=False) weak_cyclic_object = weakref.ref(cyclic_object) cyclic_object.thread.join() del cyclic_object self.assertIsNone( weak_cyclic_object(), msg=("%d references still around" % sys.getrefcount(weak_cyclic_object())) ) raising_cyclic_object = RunSelfFunction(should_raise=True) weak_raising_cyclic_object = weakref.ref(raising_cyclic_object) raising_cyclic_object.thread.join() del raising_cyclic_object self.assertIsNone( weak_raising_cyclic_object(), msg=("%d references still around" % sys.getrefcount(weak_raising_cyclic_object())), )
def check_list_refcount(self,level=5): a = UserList([1,2,3]) # temporary refcount fix until I understand why it incs by one. inline_tools.inline("a[1] = 1234;",['a']) before1 = sys.getrefcount(a) after1 = sys.getrefcount(a) assert_equal(after1,before1)
def check_set_double_new(self,level=5): a = UserDict() key = 1.0 inline_tools.inline('a[key] = 123.0;',['a','key']) assert_equal(sys.getrefcount(key),4) # should be 3 assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],123.0)
def _compare_index_result(self, arr, index, mimic_get, no_copy): """Compare mimicked result to indexing result. """ arr = arr.copy() indexed_arr = arr[index] assert_array_equal(indexed_arr, mimic_get) # Check if we got a view, unless its a 0-sized or 0-d array. # (then its not a view, and that does not matter) if indexed_arr.size != 0 and indexed_arr.ndim != 0: assert_(np.may_share_memory(indexed_arr, arr) == no_copy) # Check reference count of the original array if no_copy: # refcount increases by one: assert_equal(sys.getrefcount(arr), 3) else: assert_equal(sys.getrefcount(arr), 2) # Test non-broadcast setitem: b = arr.copy() b[index] = mimic_get + 1000 if b.size == 0: return # nothing to compare here... if no_copy and indexed_arr.ndim != 0: # change indexed_arr in-place to manipulate original: indexed_arr += 1000 assert_array_equal(arr, b) return # Use the fact that the array is originally an arange: arr.flat[indexed_arr.ravel()] += 1000 assert_array_equal(arr, b)
def test_set_complex(self): a = UserDict() key = 1+1j inline_tools.inline("a[key] = 1234;",['a','key']) assert_equal(sys.getrefcount(key),4) # should be 3 assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],1234)
def test_asarray_from_numpy_array_with_zero_copy(): obj = array_utils.create_dummy_ndarray( numpy, (2, 3), 'float32', padding=True) obj_refcount_before = sys.getrefcount(obj) a = chainerx.asarray(obj, dtype='float32') assert sys.getrefcount(obj) == obj_refcount_before + 1 chainerx.testing.assert_array_equal_ex(obj, a) # test buffer is shared (zero copy) a += a chainerx.testing.assert_array_equal_ex(obj, a) # test possibly freed memory obj_copy = obj.copy() del obj chainerx.testing.assert_array_equal_ex(obj_copy, a, strides_check=False) # test possibly freed memory (the other way) obj = array_utils.create_dummy_ndarray( numpy, (2, 3), 'float32', padding=True) a = chainerx.asarray(obj, dtype='float32') a_copy = a.copy() del a chainerx.testing.assert_array_equal_ex(a_copy, obj, strides_check=False)
def test_memoize_method(self): class foo(object): def __init__(self): self._count = 0 @memoize def wrapped(self, a, b): self._count += 1 return a + b instance = foo() refcount = sys.getrefcount(instance) self.assertEqual(instance._count, 0) self.assertEqual(instance.wrapped(1, 1), 2) self.assertEqual(instance._count, 1) self.assertEqual(instance.wrapped(1, 1), 2) self.assertEqual(instance._count, 1) self.assertEqual(instance.wrapped(2, 1), 3) self.assertEqual(instance._count, 2) self.assertEqual(instance.wrapped(1, 2), 3) self.assertEqual(instance._count, 3) self.assertEqual(instance.wrapped(1, 2), 3) self.assertEqual(instance._count, 3) self.assertEqual(instance.wrapped(1, 1), 2) self.assertEqual(instance._count, 3) # Memoization of methods is expected to not keep references to # instances, so the refcount shouldn't have changed after executing the # memoized method. self.assertEqual(refcount, sys.getrefcount(instance))
def test_nrt_nested_gen(self): def gen0(arr): for i in range(arr.size): yield arr def factory(gen0): def gen1(arr): out = np.zeros_like(arr) for x in gen0(arr): out = out + x return out, arr return gen1 py_arr = np.arange(10) c_arr = py_arr.copy() py_res, py_old = factory(gen0)(py_arr) c_gen = jit(nopython=True)(factory(jit(nopython=True)(gen0))) c_res, c_old = c_gen(c_arr) self.assertIsNot(py_arr, c_arr) self.assertIs(py_old, py_arr) self.assertIs(c_old, c_arr) np.testing.assert_equal(py_res, c_res) self.assertEqual(sys.getrefcount(py_res), sys.getrefcount(c_res))
def testBogusObject(self): def add(key, obj): self.cache[key] = obj nones = sys.getrefcount(None) key = p64(2) # value isn't persistent self.assertRaises(TypeError, add, key, 12) o = StubObject() # o._p_oid == None self.assertRaises(TypeError, add, key, o) o._p_oid = p64(3) self.assertRaises(ValueError, add, key, o) o._p_oid = key # o._p_jar == None self.assertRaises(Exception, add, key, o) o._p_jar = self.jar self.cache[key] = o # make sure it can be added multiple times self.cache[key] = o # same object, different keys self.assertRaises(ValueError, add, p64(0), o) if sys.gettrace() is None: # 'coverage' keeps track of coverage information in a data # structure that adds a new reference to None for each executed # line of code, which interferes with this test. So check it # only if we're running without coverage tracing. self.assertEqual(sys.getrefcount(None), nones)
def test_refct_mt(self): """ This test exercises the refct in multithreaded code """ def pyfunc(n, inp): out = np.empty(inp.size) for i in range(out.size): out[i] = inp[i] + 1 # Use swap to trigger many refct ops for i in range(n): out, inp = inp, out return out cfunc = nrtjit(pyfunc) size = 10 input = np.arange(size, dtype=np.float) expected_refct = sys.getrefcount(input) swapct = random.randrange(1000) expected = pyfunc(swapct, input) np.testing.assert_equal(expected, cfunc(swapct, input)) # The following checks can discover a reference count error del expected self.assertEqual(expected_refct, sys.getrefcount(input)) workers = [] outputs = [] swapcts = [] # Make wrapper to store the output def wrapped(n, input, out): out[:] = cfunc(n, input) # Create worker threads for i in range(100): out = np.empty(size) # All thread shares the same input swapct = random.randrange(1000) thread = threading.Thread(target=wrapped, args=(swapct, input, out), name="worker{0}".format(i)) workers.append(thread) outputs.append(out) swapcts.append(swapct) # Launch worker threads for thread in workers: thread.start() # Join worker threads for thread in workers: thread.join() # Check result for swapct, out in zip(swapcts, outputs): np.testing.assert_equal(pyfunc(swapct, input), out) del outputs, workers # The following checks can discover a reference count error self.assertEqual(expected_refct, sys.getrefcount(input))
def test_nrt_gen0_stop_iteration(self): """ Test cleanup on StopIteration """ pygen = nrt_gen0 cgen = jit(nopython=True)(pygen) py_ary = np.arange(1) c_ary = py_ary.copy() py_iter = pygen(py_ary) c_iter = cgen(c_ary) py_res = next(py_iter) c_res = next(c_iter) with self.assertRaises(StopIteration): py_res = next(py_iter) with self.assertRaises(StopIteration): c_res = next(c_iter) del py_iter del c_iter np.testing.assert_equal(py_ary, c_ary) self.assertEqual(py_res, c_res) # Check reference count self.assertEqual(sys.getrefcount(py_ary), sys.getrefcount(c_ary))
def test_cupy_to_chainerx_contiguous(): dtype = numpy.float32 a_cupy = cupy.arange(6, dtype=dtype).reshape((2, 3)) a_cupy_refcount_before = sys.getrefcount(a_cupy) a_chx = _fromrawpointer( a_cupy.data.mem.ptr, a_cupy.shape, a_cupy.dtype, a_cupy.strides, 'cuda:0', 0, a_cupy) assert sys.getrefcount(a_cupy) == a_cupy_refcount_before + 1 assert a_chx.device.name == 'cuda:0' chainerx.testing.assert_array_equal_ex(a_chx, a_cupy.get()) # Write to a_cupy a_cupy[0, 1] = 8 chainerx.testing.assert_array_equal_ex( a_chx, numpy.array([[0, 8, 2], [3, 4, 5]], dtype)) # Write to a_chx a_chx += 1 chainerx.testing.assert_array_equal_ex( a_cupy.get(), numpy.array([[1, 9, 3], [4, 5, 6]], dtype))
def test_swap(self): def pyfunc(x, y, t): """Swap array x and y for t number of times """ for i in range(t): x, y = y, x return x, y cfunc = nrtjit(pyfunc) x = np.random.random(100) y = np.random.random(100) t = 100 initrefct = sys.getrefcount(x), sys.getrefcount(y) expect, got = pyfunc(x, y, t), cfunc(x, y, t) self.assertIsNone(got[0].base) self.assertIsNone(got[1].base) np.testing.assert_equal(expect, got) del expect, got self.assertEqual(initrefct, (sys.getrefcount(x), sys.getrefcount(y)))
def _PrintDetail(*args): print func.__name__ print func.__doc__ print sys.getrefcount(func) start = time.time() print func(*args) print time.time() - start
def test_multiple_args_records(self): pyfunc = foobar mystruct_dt = np.dtype([('p', np.float64), ('row', np.float64), ('col', np.float64)]) mystruct = numpy_support.from_dtype(mystruct_dt) cres = compile_isolated(pyfunc, [mystruct[:], types.uint64, types.uint64], return_type=mystruct[:]) cfunc = cres.entry_point st1 = np.recarray(3, dtype=mystruct_dt) st1.p = np.arange(st1.size) + 1 st1.row = np.arange(st1.size) + 1 st1.col = np.arange(st1.size) + 1 old_refcnt_st1 = sys.getrefcount(st1) test_fail_args = ((st1, -1, 1), (st1, 1, -1)) # TypeError is for 2.6 exc_type = OverflowError if sys.version_info >= (2, 7) else TypeError for a, b, c in test_fail_args: with self.assertRaises(exc_type): cfunc(a, b, c) del test_fail_args, a, b, c gc.collect() self.assertEqual(sys.getrefcount(st1), old_refcnt_st1)
def test_lookup_commit_refcount(self): start = sys.getrefcount(self.repo) commit_sha = '5fe808e8953c12735680c257f56600cb0de44b10' commit = self.repo[commit_sha] del commit end = sys.getrefcount(self.repo) self.assertEqual(start, end)
def checkBogusObject(self): def add(key, obj): self.cache[key] = obj nones = sys.getrefcount(None) key = p64(2) # value isn't persistent self.assertRaises(TypeError, add, key, 12) o = StubObject() # o._p_oid == None self.assertRaises(TypeError, add, key, o) o._p_oid = p64(3) self.assertRaises(ValueError, add, key, o) o._p_oid = key # o._p_jar == None self.assertRaises(Exception, add, key, o) o._p_jar = self.jar self.cache[key] = o # make sure it can be added multiple times self.cache[key] = o # same object, different keys self.assertRaises(ValueError, add, p64(0), o) self.assertEqual(sys.getrefcount(None), nones)
def test_noargs_with_args_not_instantiated(self): # calling a function that doesn't take args with args should fail. # Note: difference between this test add ``test_noargs_with_args`` # below is that here Foo is not instantiated. def Foo(): return "blah" code = """ py::tuple args(2); args[0] = 1; args[1] = "hello"; return_val = Foo.call(args); """ try: first = sys.getrefcount(Foo) inline_tools.inline(code,['Foo']) except TypeError: second = sys.getrefcount(Foo) try: inline_tools.inline(code,['Foo']) except TypeError: third = sys.getrefcount(Foo) # first should == second, but the weird refcount error assert_equal(second,third)
def test_refs(self): if hasattr(sys, "getrefcount"): for tp in self._types: m = memoryview(tp(self._source)) oldrefcount = sys.getrefcount(m) m[1:2] self.assertEqual(sys.getrefcount(m), oldrefcount)
def test_set_double_new(self): a = UserDict() key = 1.0 inline_tools.inline("a[key] = 123.0;", ["a", "key"]) assert_equal(sys.getrefcount(key), 4) # should be 3 assert_equal(sys.getrefcount(a[key]), 2) assert_equal(a[key], 123.0)
def test_commit_refcount(self): commit = self.repo[COMMIT_SHA] start = sys.getrefcount(commit) tree = commit.tree del tree end = sys.getrefcount(commit) self.assertEqual(start, end)
def testGetSetUpd(self): self.assertTrue(self.snes.getUpdate() is None) upd = lambda snes, it: None refcnt = getrefcount(upd) self.snes.setUpdate(upd) self.assertEqual(getrefcount(upd), refcnt + 1) self.snes.setUpdate(upd) self.assertEqual(getrefcount(upd), refcnt + 1) self.snes.setUpdate(None) self.assertTrue(self.snes.getUpdate() is None) self.assertEqual(getrefcount(upd), refcnt) self.snes.setUpdate(upd) self.assertEqual(getrefcount(upd), refcnt + 1) upd2 = lambda snes, it: None refcnt2 = getrefcount(upd2) self.snes.setUpdate(upd2) self.assertEqual(getrefcount(upd), refcnt) self.assertEqual(getrefcount(upd2), refcnt2 + 1) tmp = self.snes.getUpdate()[0] self.assertTrue(tmp is upd2) self.assertEqual(getrefcount(upd2), refcnt2 + 2) del tmp self.snes.setUpdate(None) self.assertTrue(self.snes.getUpdate() is None) self.assertEqual(getrefcount(upd2), refcnt2)
def test04_leak_GC(self): a = 'example of private object' refcount = sys.getrefcount(a) self.obj.set_private(a) self.obj = None self.assertEqual(refcount, sys.getrefcount(a)) return
def test_leak_references_on(self): model = TesterModel() obj_ref = weakref.ref(model.root) # Initial refcount is 1 for model.root + the temporary self.assertEqual(sys.getrefcount(model.root), 2) # Iter increases by 1 do to assignment to iter.user_data res, it = model.do_get_iter([0]) self.assertEqual(id(model.root), it.user_data) self.assertEqual(sys.getrefcount(model.root), 3) # Verify getting a TreeIter more then once does not further increase # the ref count. res2, it2 = model.do_get_iter([0]) self.assertEqual(id(model.root), it2.user_data) self.assertEqual(sys.getrefcount(model.root), 3) # Deleting the iter does not decrease refcount because references # leak by default (they are stored in the held_refs pool) del it gc.collect() self.assertEqual(sys.getrefcount(model.root), 3) # Deleting a model should free all held references to user data # stored by TreeIters del model gc.collect() self.assertEqual(obj_ref(), None)
def test_to_pandas_zero_copy(): import gc arr = pyarrow.from_pylist(range(10)) for i in range(10): np_arr = arr.to_pandas() assert sys.getrefcount(np_arr) == 2 np_arr = None # noqa assert sys.getrefcount(arr) == 2 for i in range(10): arr = pyarrow.from_pylist(range(10)) np_arr = arr.to_pandas() arr = None gc.collect() # Ensure base is still valid # Because of py.test's assert inspection magic, if you put getrefcount # on the line being examined, it will be 1 higher than you expect base_refcount = sys.getrefcount(np_arr.base) assert base_refcount == 2 np_arr.sum()
def test03_leak_assignment(self) : a = "example of private object" refcount = sys.getrefcount(a) self.obj.set_private(a) self.assertEqual(refcount+1, sys.getrefcount(a)) self.obj.set_private(None) self.assertEqual(refcount, sys.getrefcount(a))
import sys a = [] b = a c = a print(sys.getrefcount(a))
def test_refs(self): for tp in self._types: m = memoryview(tp(self._source)) oldrefcount = sys.getrefcount(m) m[1:2] self.assertEqual(sys.getrefcount(m), oldrefcount)
height=0.2) if __name__ == '__main__': from ibvpy.fets.fets2D.fets2D4q import FETS2D4Q fets_sample = FETS2D4Q() fe_domain = FEGrid(coord_max=(2., 3.,), shape=(2, 3), # inactive_elems = [3], fets_eval=fets_sample) fe_domain.configure_traits() import sys print 'refcount', sys.getrefcount(fe_domain) dots = fe_domain.dots print dots.fets_eval print 'refcount', sys.getrefcount(fe_domain) print 'dof_r' print fe_domain.dof_r print fe_domain.geo_grid.cell_node_map print fe_domain.dof_grid.cell_dof_map # coord_max = (1.,1.,0.) # fe_domain.` (1,1) # fe_domain.n_nodal_dofs = 2 # print fe_domain.dof_grid.cell_dof_map print fe_domain.elem_dof_map print fe_domain.elem_X_map[0]
def make_vm( self, nodes, thunks, input_storage, output_storage, storage_map, post_thunk_clear, computed, compute_map, updated_vars, ): pre_call_clear = [storage_map[v] for v in self.no_recycling] if (self.callback is not None or self.callback_input is not None or ((config.profile or config.print_global_stats) and config.profile_memory) or (self.allow_partial_eval and not self.use_cloop)): if self.use_cloop and (self.callback is not None or self.callback_input is not None): logger.warning( "CVM does not support callback, using Stack VM.") if self.use_cloop and config.profile_memory: warnings.warn( "CVM does not support memory profile, using Stack VM.") if not self.use_cloop and self.allow_partial_eval: warnings.warn("LoopGC does not support partial evaluation, " "using Stack VM.") # Needed for allow_gc=True, profiling and storage_map reuse deps = self.compute_gc_dependencies(storage_map) vm = Stack( nodes, thunks, pre_call_clear, storage_map, compute_map, self.fgraph, self.allow_gc, len(updated_vars), dependencies=deps, callback=self.callback, callback_input=self.callback_input, ) elif self.use_cloop: # create a map from nodes to ints and vars to ints nodes_idx = {} vars_idx = {} for i, node in enumerate(nodes): nodes_idx[node] = i for v in node.inputs + node.outputs: vars_idx.setdefault(v, len(vars_idx)) for v in self.fgraph.inputs + self.fgraph.outputs: vars_idx.setdefault(v, len(vars_idx)) nodes_idx_inv = {} vars_idx_inv = {} for (node, i) in nodes_idx.items(): nodes_idx_inv[i] = node for (var, i) in vars_idx.items(): vars_idx_inv[i] = var # put storage_map and compute_map into a int-based scheme storage_map_list = [ storage_map[vars_idx_inv[i]] for i in range(len(vars_idx_inv)) ] compute_map_list = [ compute_map[vars_idx_inv[i]] for i in range(len(vars_idx_inv)) ] if nodes: assert type(storage_map_list[0]) is list assert type(compute_map_list[0]) is list # Needed for allow_gc=True, profiling and storage_map reuse dependency_map = self.compute_gc_dependencies(storage_map) dependency_map_list = [[ vars_idx[d] for d in dependency_map[vars_idx_inv[i]] ] for i in range(len(vars_idx_inv))] # build the pointers to node inputs and offsets base_input_output_list = [] node_n_inputs = [] node_n_outputs = [] node_input_offset = [] node_output_offset = [] for node in nodes: inputs_idx = [vars_idx[v] for v in node.inputs] outputs_idx = [vars_idx[v] for v in node.outputs] node_n_inputs.append(len(inputs_idx)) node_n_outputs.append(len(outputs_idx)) node_input_offset.append(len(base_input_output_list)) base_input_output_list.extend(inputs_idx) node_output_offset.append(len(base_input_output_list)) base_input_output_list.extend(outputs_idx) # build the var owner array var_owner = [None] * len(vars_idx) for (var, i) in vars_idx.items(): if var.owner: var_owner[i] = nodes_idx[var.owner] is_lazy_list = [int(th.lazy) for th in thunks] output_vars = [vars_idx[v] for v in self.fgraph.outputs] # builds the list of prereqs induced by e.g. destroy_handler ords = self.fgraph.orderings() node_prereqs = [] node_output_size = [] for i, node in enumerate(nodes): node_output_size.append(0) prereq_var_idxs = [] for prereq_node in ords.get(node, []): prereq_var_idxs.extend( [vars_idx[v] for v in prereq_node.outputs]) prereq_var_idxs = list(set(prereq_var_idxs)) prereq_var_idxs.sort() # TODO: why sort? node_prereqs.append(prereq_var_idxs) # Builds the list of input storage to update (according to update # rules) when the outputs are computed. # They are in the same order as the second part of output_vars # (output_vars contains first the returned outputs, then the # values of the update expressions). update_storage = [] update_in_from_out = {} for (ivar, ovar) in updated_vars.items(): update_in_from_out[vars_idx[ovar]] = vars_idx[ivar] for oidx in output_vars: if oidx in update_in_from_out: update_storage.append(update_in_from_out[oidx]) # PyPy has no sys.getrefcount, so ignore this check if not running # under CPython. if platform.python_implementation() == "CPython": c0 = sys.getrefcount(node_n_inputs) vm = CVM( nodes, thunks, pre_call_clear, allow_gc=self.allow_gc, call_counts=[0] * len(nodes), call_times=[0.0] * len(nodes), compute_map_list=compute_map_list, storage_map_list=storage_map_list, base_input_output_list=base_input_output_list, node_n_inputs=node_n_inputs, node_n_outputs=node_n_outputs, node_input_offset=node_input_offset, node_output_offset=node_output_offset, var_owner=var_owner, is_lazy_list=is_lazy_list, output_vars=output_vars, node_prereqs=node_prereqs, node_output_size=node_output_size, update_storage=update_storage, dependencies=dependency_map_list, ) if platform.python_implementation() == "CPython": assert c0 == sys.getrefcount(node_n_inputs) else: lazy = self.lazy if lazy is None: lazy = config.vm.lazy if lazy is None: lazy = not all([(not th.lazy) for th in thunks]) if not lazy: # there is no conditional in the graph if self.allow_gc: vm = LoopGC( nodes, thunks, pre_call_clear, post_thunk_clear, ) else: vm = Loop( nodes, thunks, pre_call_clear, ) else: # Needed when allow_gc=True and profiling deps = self.compute_gc_dependencies(storage_map) vm = Stack( nodes, thunks, pre_call_clear, storage_map, compute_map, self.fgraph, self.allow_gc, len(updated_vars), dependencies=deps, ) return vm
print(hex(id(i1)), hex(id(i2))) l1 = [1, 2, 3] l2 = [1, 2, 3] print(hex(id(l1)), hex(id(l2))) s1 = 'hello' s2 = 'hello' print(hex(id(s1)), hex(id(s2))) # is (동일 레퍼런스 비교 print(i1 is i2) # immutable print(l1 is l2) # mutable print(s1 is s2) # immutable # ????? t1 = (1, 2, 3) t2 = (1, 2, 3) print(sys.getrefcount(t1), sys.getrefcount(t2)) # 왜 5 왜 5 왜 5 왜5 왜5 print(t1 is t2) # 리터럴로 생성하면 같은 객체가 생성되나 명시하여 생성하면 다른 객체가 생성된다. t3 = tuple(range(1, 4)) print(sys.getrefcount(t3)) print(t1 is t3) # 슬라이싱으로 생성해도 다른 객체이다. t4 = (0, 1, 2, 3)[1:] print(t1 is t4)
# coding=utf-8 # a引用了对象b class from_obj(object): def __init__(self, to_obj): self.to_obj = to_obj b = [1, 2, 3] a = from_obj(b) print(id(a.to_obj)) print(id(b)) # 当一个对象A被另一个对象B引用时,A的引用计数将增加1。 from sys import getrefcount a = [1, 2, 3] print(getrefcount(a)) b = [a, a] print(getrefcount(a))
def _cmpMRCN(a, b): '''compare two named modules by refcount and then name''' r = cmp(sys.getrefcount(sys.modules[b]), sys.getrefcount(sys.modules[a])) if not r: r = cmp(b, a) return r
import sys a = [] print sys.getrefcount(a) b = a print sys.getrefcount(a) c = b print sys.getrefcount(a) del b print sys.getrefcount(a) del c print sys.getrefcount(a)
# variables are objs in memory: name1 = 'mojtaba' name2 = 'leyla' name3 = name2 name1_hex_id = hex(id(name1)) name2_hex_id = hex(id(name2)) name3_hex_id = hex(id(name3)) print(name1_hex_id) print(name2_hex_id) print(name3_hex_id) # reference counting in python # using the sys.getrefcount function increases the ref count import sys name1_ref_count = sys.getrefcount(name1) name2_ref_count = sys.getrefcount(name2) print(name1_ref_count) print(name2_ref_count) # However, using this function does not increase the referenece count: import ctypes name1_ref_count_via_ctypes = ctypes.c_long.from_address(id(name1)).value name2_ref_count_via_ctypes = ctypes.c_long.from_address(id(name2)).value print(name1_ref_count_via_ctypes) print(name2_ref_count_via_ctypes) # let's define a function to use for the abovementioned task: def count_references(address: int):
def assertRefEqual(self, v1, v2, msg=None): self.assertEqual(sys.getrefcount(v1), sys.getrefcount(v2), msg)
def test_refcount1(): i = numpy.arange(12, dtype="i4").reshape(3, 4) assert sys.getrefcount(i) == 2 i2 = awkward1.layout.Identities32(awkward1.layout.Identities32.newref(), [(0, "hey"), (1, "there")], i) assert (sys.getrefcount(i), sys.getrefcount(i2)) == (3, 2) tmp = numpy.asarray(i2) assert tmp.tolist() == [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] assert (sys.getrefcount(i), sys.getrefcount(i2)) == (3, 2 + 1 * py27) del tmp assert (sys.getrefcount(i), sys.getrefcount(i2)) == (3, 2) tmp2 = i2.array assert tmp2.tolist() == [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] assert (sys.getrefcount(i), sys.getrefcount(i2)) == (3, 2 + 1 * py27) del tmp2 assert (sys.getrefcount(i), sys.getrefcount(i2)) == (3, 2) del i2 assert sys.getrefcount(i) == 2
def tearDown(self): # Remove the module. import deprecation_example as mod del sys.modules[mod.__name__] self.assertEqual(sys.getrefcount(mod), 2)
#!/usr/bin/env python3 # encoding: utf-8 #end_pymotw_header import sys one = [] print('At start :', sys.getrefcount(one)) two = one print('Second reference :', sys.getrefcount(one)) del two print('After del :', sys.getrefcount(one))
def testReference(self): o = QtCore.QObject() m = MyObject(o) self.assertEqual(sys.getrefcount(o), 3) del m self.assertEqual(sys.getrefcount(o), 2)
# # # a = B() # # # # print(sys.getrefcount(a)) # # # b = B() # print(sys.getrefcount(b)) # d1 = {'b': weakref.ref(b)} # # print(sys.getrefcount(b)) # print(d1) # # del b # print(d1) # print(d1['b']) # print(dir(d1['b'])) class C: def __init__(self): self.name = self.__class__.__name__ c = C() print(sys.getrefcount(c)) d2 = {'c': weakref.proxy(c)} print(sys.getrefcount(c)) # del c print(d2['c'].name) print(sys.getrefcount(c))
""" Created by Matic Kukovec """ # Import the PyQt5 module with some of the GUI widgets import PyQt5.QtWidgets # Import the QScintilla module import PyQt5.Qsci # Import Python's sys module needed to get the application arguments import sys # Create the main PyQt application object application = PyQt5.QtWidgets.QApplication(sys.argv) # Create a QScintila editor instance editor = PyQt5.Qsci.QsciScintilla() print(sys.getrefcount(editor)) # Show the editor editor.show() # Put the “Hello World” text into the editing area of the editor editor.setText("Hello World") # Execute the application application.exec_()
import sys class test: pass t1 = test() t2 = t1 t3 = t1 t4 = t1 print(sys.getrefcount(t1))
def test_array_ref_to_ndarray_base(): arr = np.array([1, 2, 3]) refcount = sys.getrefcount(arr) arr2 = pa.array(arr) # noqa assert sys.getrefcount(arr) == (refcount + 1)
def test_noremove(self): keep = set(k for k, v in self.d.items() if sys.getrefcount(v) > 3) assert keep == {'referenced', 'dangling'}
def test_refcount(): content = ak.layout.NumpyArray(np.array([1.1, 2.2, 3.3])) offsets = ak.layout.Index32(np.array([0, 2, 2, 3], "i4")) array = ak.layout.ListOffsetArray32(offsets, content) assert (sys.getrefcount(content), sys.getrefcount(array)) == (2, 2) iter1 = iter(content) assert (sys.getrefcount(content), sys.getrefcount(array)) == (2, 2) x1 = next(iter1) assert (sys.getrefcount(content), sys.getrefcount(array)) == (2, 2) iter2 = iter(array) assert (sys.getrefcount(content), sys.getrefcount(array)) == (2, 2) x2 = next(iter2) assert (sys.getrefcount(content), sys.getrefcount(array)) == (2, 2) del iter1 del x1 assert (sys.getrefcount(content), sys.getrefcount(array)) == (2, 2) del iter2 del x2 assert (sys.getrefcount(content), sys.getrefcount(array)) == (2, 2)
import sys sys.path.insert(0, 'pyds%d%d' % (sys.version_info[0], sys.version_info[1])) import pycxx_iter IT = pycxx_iter.IterT(5, 7) for i in IT: print i, IT print "refcount of IT:", sys.getrefcount(IT)
def test_remove(self): keep = set(k for k, v in self.d.items() if sys.getrefcount(v) > 4) assert keep == {'referenced'}
def test_arrow_basics(): offset = 5 ar = pa.array(['aap', 'noot', None, 'mies']) bitmap_buffer, offsets, string_bytes = ar.buffers() indices = np.frombuffer(offsets, np.int32, len(offsets) // 4) + offset null_bitmap = np.frombuffer(bitmap_buffer, np.uint8, len(bitmap_buffer)) bytes_strings = np.frombuffer(string_bytes, 'S1', len(string_bytes)) # indices = np.frombuffer(offsets, np.int32, len(offsets)//4) # ref counts start at 2 assert sys.getrefcount(bytes_strings) == 2 assert sys.getrefcount(indices) == 2 sl = vaex.strings.StringList32(bytes_strings, indices, len(ar), offset, null_bitmap, 0) assert sys.getrefcount(sl) == 2 # we should keep a reference only assert sys.getrefcount(bytes_strings) == 3 assert sys.getrefcount(indices) == 3 assert sl.get(0) == "aap" assert sl.get(1) == "noot" assert sl.get(2) == None assert sl.get(3) == "mies" # getting a string creates a new object s = sl.get(3) assert sys.getrefcount(s) == 2 string_list = sl.to_numpy() assert sys.getrefcount(string_list) == 2 # internally in the list, s, and in getrefcount s = string_list[0] assert sys.getrefcount(s) == 3 assert list(sl.to_numpy()) == ["aap", "noot", None, "mies"] string_list = sl.to_numpy() c = sl.capitalize() assert list(c.to_numpy()) == ["Aap", "Noot", None, "Mies"] c = sl.pad(5, ' ', True, True) assert list(c.to_numpy()) == [" aap ", " noot", None, " mies"] c = sl.slice_string_end(1) assert list(c.to_numpy()) == ["ap", "oot", None, "ies"] assert sys.getrefcount(sl) == 2 c = sl.lower() assert list(c.to_numpy()) == ["aap", "noot", None, "mies"] c = sl.upper() assert list(c.to_numpy()) == ["AAP", "NOOT", None, "MIES"] c = sl.count("a", False) assert c.tolist() == [2, 0, 0, 0] sl2 = vaex.strings.StringList32(sl.bytes, sl.indices, sl.length, sl.offset, null_bitmap, 0) assert list(sl2.to_numpy()) == ["aap", "noot", None, "mies"] # ds2.columns['name_arrow'][:].string_sequence.slice(0,5).indices assert sys.getrefcount(sl) == 2 sl_slice = sl.slice(1, 4) assert list(sl_slice.to_numpy()) == ["noot", None, "mies"] assert sys.getrefcount(sl) == 3 del sl_slice assert sys.getrefcount(sl) == 2 sl_slice = sl.slice(0, 3) assert list(sl_slice.to_numpy()) == ["aap", "noot", None] offset2 = 11 indices2 = indices.copy() indices2[:] = 1 null_bitmap2 = null_bitmap.copy() null_bitmap2[:] = 0xff bytes_strings2 = bytes_strings.copy() bytes_strings2[:] = b'?' assert sys.getrefcount(indices) == 3 assert sys.getrefcount(bytes_strings) == 3 sl_copy = vaex.strings.StringList32(bytes_strings2, indices2, len(ar), offset2, null_bitmap2, 0) sl_copy.fill_from(sl) assert list(sl_copy.to_numpy()) == ["aap", "noot", None, "mies"] # test fill_from with unequal offset offset = 110 ar3 = pa.array([None, 'NOOT', 'MIES']) bitmap_buffer3, offsets3, string_bytes3 = ar3.buffers() indices3 = np.frombuffer(offsets3, np.int32, len(offsets3) // 4) + offset null_bitmap3 = np.frombuffer(bitmap_buffer3, np.uint8, len(bitmap_buffer3)) bytes_strings3 = np.frombuffer(string_bytes3, 'S1', len(string_bytes3)) sl_upper = vaex.strings.StringList32(bytes_strings3, indices3, len(ar3), offset, null_bitmap3, 0) sl14 = sl.slice(1, 4) assert sl14.offset != sl_upper.offset assert sl14.fill_from(sl_upper) == 4 + 4 assert list(sl14.to_numpy()) == [None, "NOOT", "MIES"] sl14 = sl.slice(1, 4, 3) assert sl14.offset != sl_upper.offset assert sl14.fill_from(sl_upper) == 4 + 4 assert list(sl14.to_numpy()) == [None, "NOOT", "MIES"] # sl14 and sl_slice keep a reference to sl del sl assert sys.getrefcount(indices) == 3 assert sys.getrefcount(bytes_strings) == 3 del sl14 del sl_slice assert sys.getrefcount(indices) == 2 assert sys.getrefcount(bytes_strings) == 2
4、对象所在容器被销毁,或者从容器中删除。 """ import sys # print(sys.getrefcount("hello")) # a = "hello" # print(sys.getrefcount("hello")) # b = a # print(sys.getrefcount("hello")) # del b, a # print(sys.getrefcount("hello")) listA = [x for x in range(1000)] listB = [x for x in range(1000)] print(sys.getrefcount(listA)) print(sys.getrefcount(listB)) # listA的引用计数加一,listB不变 listB.append(listA) print(sys.getrefcount(listA)) print(sys.getrefcount(listB)) # 内存泄露:在程序运行过程中,产生的始终无法访问到的内存地址 # 在回收内存之前 进行标记 """ 标记清除:在进行清除变量之后对每一个删除的对象引用计数-1 如果引用计数变为0 就暂时放入死亡容器 如果引用计数部位0 检测是否引用到了死亡容器容器中对象 如果引用到了死亡容器对象,将死亡容器对象拿出来放入存活容器 """
def InternalConfigure(self, params): ''' Configure ''' # Read other parameters self.namedata = reader.read_param(params, 'variables', "required") self.N = reader.read_param(params, 'N', 'N') self.eff_eqn = reader.read_param(params, 'efficiency', '1') self.bins = reader.read_param(params, 'bins', []) self.asInteger = reader.read_param(params, 'asInteger', False) self.energy_or_frequency = reader.read_param( params, 'energy_or_frequency', 'energy') #Currently only set up to use frequency self.efficiency_filepath = reader.read_param(params, 'efficiency_filepath', '') self.fss_bins = reader.read_param(params, "fss_bins", False) # If self.fss_bins is True, self.bins is ignored and overwritten # initialize the histogram to store the corrected data if self.energy_or_frequency == 'energy': print(sys.getrefcount(self.corrected_data)) self.output_bin_variable = 'KE' elif self.energy_or_frequency == 'frequency': self.output_bin_variable = 'F' else: return False self.efficiency_file_content = self.GetEfficiencyFileContent() if not self.efficiency_file_content == self.GetEfficiencyFileContent(): logger.error("Failed reading efficiency file") return False if self.fss_bins == True: self.bin_centers = self.efficiency_file_content['frequencies'] self.bins = np.array(self.bin_centers) - (self.bin_centers[1] - self.bin_centers[0]) / 2 self.bins = np.append(self.bins, [ self.bin_centers[-1] + (self.bin_centers[1] - self.bin_centers[0]) / 2 ]) else: self.bin_centers = self.bins[0:-1] + 0.5 * (self.bins[1] - self.bins[0]) # check that frequency bins are withing good frequency region if self.bins[-1] > np.max( self.efficiency_file_content['frequencies']): logger.error( 'Bin edge above FSS frequency region. FSS region is {} - {} GHz' .format( np.min(self.efficiency_file_content['frequencies']) * 1e-9, np.max(self.efficiency_file_content['frequencies']) * 1e-9)) return False elif self.bins[0] < np.min( self.efficiency_file_content['frequencies']): logger.warning( 'Bin edge below FSS frequency region. As long as tritium endpoint is higher (in frequency) this is not a problem. FSS region is {} - {} GHz' .format( np.min(self.efficiency_file_content['frequencies']) * 1e-9, np.max(self.efficiency_file_content['frequencies']) * 1e-9)) return True
def saveOptimizedGarbage(path): import gc import sys import inspect delimiter = '=-=' * 100 + '\n' logPattern = '{}Representation str(garbageObject):\n{}\nObject type: {}\nRef count: {}\nModule of object: {}\n' gc.collect() gcGarbageCopy = gc.garbage[:] del gc.garbage[:] with open(path, 'w') as f: for garbageObject in gcGarbageCopy: try: f.write(logPattern.format(delimiter, str(garbageObject), type(garbageObject), sys.getrefcount(garbageObject), inspect.getmodule(garbageObject))) except: continue del gcGarbageCopy[:]
def test_buffer(self): import pytest import sys mod = self.make_module(""" @TYPE_STRUCT_BEGIN(FakeArrayObject) int exports; @TYPE_STRUCT_END static char static_mem[12] = {0,1,2,3,4,5,6,7,8,9,10,11}; static HPy_ssize_t _shape[1] = {12}; static HPy_ssize_t _strides[1] = {1}; HPyDef_SLOT(FakeArray_getbuffer, _getbuffer_impl, HPy_bf_getbuffer) static int _getbuffer_impl(HPyContext *ctx, HPy self, HPy_buffer* buf, int flags) { FakeArrayObject *arr = FakeArrayObject_AsStruct(ctx, self); if (arr->exports > 0) { buf->obj = HPy_NULL; HPyErr_SetString(ctx, ctx->h_BufferError, "only one buffer allowed"); return -1; } arr->exports++; buf->buf = static_mem; buf->len = 12; buf->itemsize = 1; buf->readonly = 1; buf->ndim = 1; buf->format = "B"; buf->shape = _shape; buf->strides = _strides; buf->suboffsets = NULL; buf->internal = NULL; buf->obj = HPy_Dup(ctx, self); return 0; } HPyDef_SLOT(FakeArray_releasebuffer, _relbuffer_impl, HPy_bf_releasebuffer) static void _relbuffer_impl(HPyContext *ctx, HPy h_obj, HPy_buffer* buf) { FakeArrayObject *arr = FakeArrayObject_AsStruct(ctx, h_obj); arr->exports--; } static HPyDef *FakeArray_defines[] = { &FakeArray_getbuffer, &FakeArray_releasebuffer, NULL }; static HPyType_Spec FakeArray_Spec = { .name = "mytest.FakeArray", .basicsize = sizeof(FakeArrayObject), .defines = FakeArray_defines, .legacy = FakeArrayObject_IS_LEGACY, }; @EXPORT_TYPE("FakeArray", FakeArray_Spec) @INIT """) arr = mod.FakeArray() if self.supports_refcounts(): init_refcount = sys.getrefcount(arr) with memoryview(arr) as mv: with pytest.raises(BufferError): mv2 = memoryview(arr) if self.supports_refcounts(): assert sys.getrefcount(arr) == init_refcount + 1 for i in range(12): assert mv[i] == i if self.supports_refcounts(): assert sys.getrefcount(arr) == init_refcount mv2 = memoryview(arr) # doesn't raise
def getrefcount(*args): return sys.getrefcount(*args)
def test_transform_bidirectional(self): test_data = object() def transform_to(binding, value, user_data=None): self.assertEqual(user_data, test_data) return value * 2 def transform_from(binding, value, user_data=None): self.assertEqual(user_data, test_data) return value // 2 test_data_ref_count = sys.getrefcount(test_data) transform_to_ref_count = sys.getrefcount(transform_to) transform_from_ref_count = sys.getrefcount(transform_from) # bidirectional bindings binding = self.source.bind_property('int_prop', self.target, 'int_prop', GObject.BindingFlags.BIDIRECTIONAL, transform_to, transform_from, test_data) binding = binding # PyFlakes binding_ref_count = sys.getrefcount(binding) binding_gref_count = binding.__grefcount__ self.source.int_prop = 1 self.assertEqual(self.source.int_prop, 1) self.assertEqual(self.target.int_prop, 2) self.target.props.int_prop = 4 self.assertEqual(self.source.int_prop, 2) self.assertEqual(self.target.int_prop, 4) self.assertEqual(sys.getrefcount(binding), binding_ref_count) self.assertEqual(binding.__grefcount__, binding_gref_count) # test_data ref count increases by 2, once for each callback. self.assertEqual(sys.getrefcount(test_data), test_data_ref_count + 2) self.assertEqual(sys.getrefcount(transform_to), transform_to_ref_count + 1) self.assertEqual(sys.getrefcount(transform_from), transform_from_ref_count + 1) # Unbind should clear out the binding and its transforms binding.unbind() # Setting source or target should not change the other. self.target.int_prop = 3 self.source.int_prop = 5 self.assertEqual(self.target.int_prop, 3) self.assertEqual(self.source.int_prop, 5) self.assertEqual(sys.getrefcount(test_data), test_data_ref_count) self.assertEqual(sys.getrefcount(transform_to), transform_to_ref_count) self.assertEqual(sys.getrefcount(transform_from), transform_from_ref_count)
def check_ref_counts(a_dict): print ('Checking ref counts on', a_dict) print ('\tmsg:', sys.getrefcount(a_dict)) for d in a_dict: print ('\t',d, sys.getrefcount(d))