def example_one(): """ File "/home/mutwa/projects/effective_python_tips/item_59/waste_memory.py", line 7 self.x = os.urandom(100) File "/home/mutwa/projects/effective_python_tips/item_59/waste_memory.py", line 14 obj = MyObject() File "/home/mutwa/projects/effective_python_tips/item_59/waste_memory.py", line 22 deep_values.append(get_data()) File "with_trace.py", line 9 x = waste_memory.run() File "with_trace.py", line 18 example_one() File "with_trace.py", line 22 main() - notes for Nick - a stack trace like this is most valuable for figuring out which particular usage of a common function is responsible for memory consumption in a program. """ tracemalloc.start(10) time1 = tracemalloc.take_snapshot() x = waste_memory.run() time2 = tracemalloc.take_snapshot() stats = time2.compare_to(time1, 'traceback') top = stats[0] print('\n'.join(top.traceback.format()))
def example_one(): """ /path/to/waste_memory.py:7: size=2392 KiB (+2392 KiB), count=29987 (+29987), average=82 B /path/to/waste_memory.py:8: size=869 KiB (+869 KiB), count=10000 (+10000), average=89 B /path/to/waste_memory.py:14: size=547 KiB (+547 KiB), count=10000 (+10000), average=56 B """ tracemalloc.start(10) # Save upto 10 stack frames time1 = tracemalloc.take_snapshot() x = waste_memory.run() time2 = tracemalloc.take_snapshot() stats = time2.compare_to(time1, 'lineno') for stat in stats[:3]: print(stat)
def example_one(): """ 5165 objects before 15178 objects after <waste_memory.MyObject object at 0x7f2b059c4518> <waste_memory.MyObject object at 0x7f2b059c4550> <waste_memory.MyObject object at 0x7f2b059c4588> - notes for Nick - the problem with g.get_objects is that it doesn't tell you anything about how the objects were allocated. """ found_objects = gc.get_objects() print('%d objects before' % len(found_objects)) x = waste_memory.run() found_objects = gc.get_objects() print('%d objects after' % len(found_objects)) for obj in found_objects[:3]: print(repr(obj)[:100])
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Preamble to mimick book environment import logging from pprint import pprint from sys import stdout as STDOUT # Example 3 import tracemalloc tracemalloc.start(10) # Save up to 10 stack frames time1 = tracemalloc.take_snapshot() import waste_memory x = waste_memory.run() time2 = tracemalloc.take_snapshot() stats = time2.compare_to(time1, 'lineno') for stat in stats[:3]: print(stat)
#!/usr/bin/env python3.8 # top_n.py import tracemalloc tracemalloc.start(10) # 스택 깊이 설정 time1 = tracemalloc.take_snapshot() # 이전 스냅샷 import waste_memory x = waste_memory.run() # Usage to debug time2 = tracemalloc.take_snapshot() # 이후 스냅샷 stats = time2.compare_to(time1, 'lineno') # 두 스냅샷을 비교 for stat in stats[:3]: print(stat)
# Copyright 2014-2019 Brett Slatkin, Pearson Education Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import gc found_objects = gc.get_objects() print('Before:', len(found_objects)) import waste_memory hold_reference = waste_memory.run() found_objects = gc.get_objects() print('After: ', len(found_objects)) for obj in found_objects[:3]: print(repr(obj)[:100]) print('...')