import gc import tracemalloc import sqlite3 import os FILTERS = [tracemalloc.Filter(False, tracemalloc.__file__, all_frames=True)] tracemalloc.start() before = tracemalloc.take_snapshot() before = before.filter_traces(FILTERS) for index in range(10): try: os.unlink('./test.db3') except OSError: pass connection = sqlite3.connect('./test.db3') cursor = connection.cursor() cursor.execute('CREATE TABLE test_insert (id ROWID, value INTEGER);') query = 'INSERT INTO test_insert (value) VALUES (%s)' for entry in range(1, 100): cursor.execute(query % entry) connection.commit() cursor.close() connection.close() gc.collect() after = tracemalloc.take_snapshot() after = after.filter_traces(FILTERS) top_stats = after.compare_to(before, 'lineno') print("[ Top 3 lines ]") for stat in top_stats[:3]: print(stat)