@@ -159,3 +159,87 @@ A better technique would be to use a ``PyCapsule`` as a base object:
159
159
return NULL;
160
160
}
161
161
...
162
+
163
+ Example of memory tracing with ``np.lib.tracemalloc_domain ``
164
+ ------------------------------------------------------------
165
+
166
+ Note that since Python 3.6 (or newer), the builtin ``tracemalloc`` module can be used to
167
+ track allocations inside NumPy. NumPy places its CPU memory allocations into the
168
+ ``np.lib.tracemalloc_domain`` domain.
169
+ For additional information, check: `https:// docs.python.org/3/library/tracemalloc.html`.
170
+
171
+ Here is an example on how to use ``np.lib.tracemalloc_domain``:
172
+
173
+ .. code-block:: python
174
+
175
+ """
176
+ The goal of this example is to show how to trace memory
177
+ from an application that has NumPy and non-NumPy sections.
178
+ We only select the sections using NumPy related calls.
179
+ """
180
+
181
+ import tracemalloc
182
+ import numpy as np
183
+
184
+ # Flag to determine if we select NumPy domain
185
+ use_np_domain = True
186
+
187
+ nx = 300
188
+ ny = 500
189
+
190
+ # Start to trace memory
191
+ tracemalloc.start()
192
+
193
+ # Section 1
194
+ # ---------
195
+
196
+ # NumPy related call
197
+ a = np.zeros((nx,ny))
198
+
199
+ # non-NumPy related call
200
+ b = [i**2 for i in range(nx*ny)]
201
+
202
+ snapshot1 = tracemalloc.take_snapshot()
203
+ # We filter the snapshot to only select NumPy related calls
204
+ np_domain = np.lib.tracemalloc_domain
205
+ dom_filter = tracemalloc.DomainFilter(inclusive=use_np_domain,
206
+ domain=np_domain)
207
+ snapshot1 = snapshot1.filter_traces([dom_filter])
208
+ top_stats1 = snapshot1.statistics('traceback')
209
+
210
+ print("================ SNAPSHOT 1 =================")
211
+ for stat in top_stats1:
212
+ print(f"{stat.count} memory blocks: {stat.size / 1024:.1f} KiB")
213
+ print(stat.traceback.format()[-1])
214
+
215
+ # Clear traces of memory blocks allocated by Python
216
+ # before moving to the next section.
217
+ tracemalloc.clear_traces()
218
+
219
+ # Section 2
220
+ #----------
221
+
222
+ # We are only using NumPy
223
+ c = np.sum(a*a)
224
+
225
+ snapshot2 = tracemalloc.take_snapshot()
226
+ top_stats2 = snapshot2.statistics('traceback')
227
+
228
+ print()
229
+ print("================ SNAPSHOT 2 =================")
230
+ for stat in top_stats2:
231
+ print(f"{stat.count} memory blocks: {stat.size / 1024:.1f} KiB")
232
+ print(stat.traceback.format()[-1])
233
+
234
+ tracemalloc.stop()
235
+
236
+ print()
237
+ print("============================================")
238
+ print("\n Tracing Status : ", tracemalloc.is_tracing())
239
+
240
+ try:
241
+ print("\n Trying to Take Snapshot After Tracing is Stopped.")
242
+ snap = tracemalloc.take_snapshot()
243
+ except Exception as e:
244
+ print("Exception : ", e)
245
+
0 commit comments