Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9713a4c

Browse filesBrowse files
committed
nb to explore garbage collection
1 parent 7e087db commit 9713a4c
Copy full SHA for 9713a4c

File tree

Expand file treeCollapse file tree

1 file changed

+345
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+345
-0
lines changed

‎examples/garbage_collection.ipynb

Copy file name to clipboard
+345Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "1ef0578e-09e1-45ff-bd34-84472db3885e",
7+
"metadata": {
8+
"tags": []
9+
},
10+
"outputs": [],
11+
"source": [
12+
"from fastplotlib import Plot\n",
13+
"import numpy as np\n",
14+
"import sys\n",
15+
"\n",
16+
"import weakref\n",
17+
"import gc\n",
18+
"import os, psutil\n",
19+
"process = psutil.Process(os.getpid())"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"id": "1bb6bc6f-7786-4d23-9eb1-e30bbc66c798",
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"print(process.memory_info().rss / 1024 / 1024)"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"id": "b23ba640-88ec-40d9-b53c-c8cbb3e39b0b",
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"plot = Plot()\n",
40+
"\n",
41+
"# a = np.random.rand(5_000_000)\n",
42+
"# plot.add_line(a)\n",
43+
"\n",
44+
"\n",
45+
"plot.show()"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"id": "17f46f21-b29d-4dd3-9496-989bbb240f50",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"print(process.memory_info().rss / 1024 / 1024)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"id": "e26d392f-6afd-4e89-a685-d618065d3caf",
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"a = np.random.rand(1_000, 5_000)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"id": "31e3027a-56cf-4f7b-ba78-aed4f78eef47",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"print(process.memory_info().rss / 1024 / 1024)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"id": "9cf2ceaa-5b01-4e12-be39-f267cd355833",
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"plot.add_line_stack(a)"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "53170858-ae72-4451-8647-7d5b1f9da75e",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"print(process.memory_info().rss / 1024 / 1024)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"id": "ee2481c9-82e3-4043-85fd-21a0cdf21187",
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"plot.auto_scale()"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"id": "e4e0f73b-c58a-40e7-acf5-07a1f70d2821",
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"plot.delete_graphic(plot.graphics[0])"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"id": "56c64498-229e-48b7-9fb1-f7c327fff2ae",
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"print(process.memory_info().rss / 1024 / 1024)"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"id": "dd6a26c1-ea81-469d-ae7a-95839b1f9d5a",
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"import numpy as np\n",
136+
"from wgpu.gui.auto import WgpuCanvas, run\n",
137+
"import pygfx as gfx\n",
138+
"import subprocess\n",
139+
"\n",
140+
"canvas = WgpuCanvas()\n",
141+
"renderer = gfx.WgpuRenderer(canvas)\n",
142+
"scene = gfx.Scene()\n",
143+
"camera = gfx.OrthographicCamera(5000, 5000)\n",
144+
"camera.position.x = 2048\n",
145+
"camera.position.y = 2048\n",
146+
"\n",
147+
"\n",
148+
"def make_image():\n",
149+
" data = np.random.rand(4096, 4096).astype(np.float32)\n",
150+
"\n",
151+
" return gfx.Image(\n",
152+
" gfx.Geometry(grid=gfx.Texture(data, dim=2)),\n",
153+
" gfx.ImageBasicMaterial(clim=(0, 1)),\n",
154+
" )\n",
155+
"\n",
156+
"\n",
157+
"class Graphic:\n",
158+
" def __init__(self):\n",
159+
" data = np.random.rand(4096, 4096).astype(np.float32)\n",
160+
" self.wo = gfx.Image(\n",
161+
" gfx.Geometry(grid=gfx.Texture(data, dim=2)),\n",
162+
" gfx.ImageBasicMaterial(clim=(0, 1)),\n",
163+
" )\n",
164+
"\n",
165+
"\n",
166+
"def draw():\n",
167+
" renderer.render(scene, camera)\n",
168+
" canvas.request_draw()\n",
169+
"\n",
170+
"\n",
171+
"def print_nvidia(msg):\n",
172+
" print(msg)\n",
173+
" print(\n",
174+
" subprocess.check_output([\"nvidia-smi\", \"--format=csv\", \"--query-gpu=memory.used\"]).decode().split(\"\\n\")[1]\n",
175+
" )\n",
176+
" print()\n",
177+
"\n",
178+
"\n",
179+
"def add_img(*args):\n",
180+
" print_nvidia(\"Before creating image\")\n",
181+
" img = make_image()\n",
182+
" print_nvidia(\"After creating image\")\n",
183+
" scene.add(img)\n",
184+
" img.add_event_handler(remove_img, \"click\")\n",
185+
" draw()\n",
186+
" print_nvidia(\"After add image to scene\")\n",
187+
"\n",
188+
"\n",
189+
"def remove_img(*args):\n",
190+
" img = scene.children[0]\n",
191+
" scene.remove(img)\n",
192+
" draw()\n",
193+
" print_nvidia(\"After remove image from scene\")\n",
194+
" del img\n",
195+
" draw()\n",
196+
" print_nvidia(\"After del image\")\n",
197+
"\n",
198+
"\n",
199+
"renderer.add_event_handler(add_img, \"double_click\")\n",
200+
"canvas"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"id": "2599f430-8b00-4490-9e11-774897be6e77",
207+
"metadata": {},
208+
"outputs": [],
209+
"source": [
210+
"import numpy as np\n",
211+
"from wgpu.gui.auto import WgpuCanvas, run\n",
212+
"import pygfx as gfx\n",
213+
"import subprocess\n",
214+
"\n",
215+
"canvas = WgpuCanvas()\n",
216+
"renderer = gfx.WgpuRenderer(canvas)\n",
217+
"scene = gfx.Scene()\n",
218+
"camera = gfx.OrthographicCamera(5000, 5000)\n",
219+
"camera.position.x = 2048\n",
220+
"camera.position.y = 2048\n",
221+
"\n",
222+
"\n",
223+
"def make_image():\n",
224+
" data = np.random.rand(4096, 4096).astype(np.float32)\n",
225+
"\n",
226+
" return gfx.Image(\n",
227+
" gfx.Geometry(grid=gfx.Texture(data, dim=2)),\n",
228+
" gfx.ImageBasicMaterial(clim=(0, 1)),\n",
229+
" )"
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": null,
235+
"id": "3ec10f26-6544-4ad3-80c1-aa34617dc826",
236+
"metadata": {},
237+
"outputs": [],
238+
"source": [
239+
"import weakref"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": null,
245+
"id": "acc819a3-cd50-4fdd-a0b5-c442d80847e2",
246+
"metadata": {},
247+
"outputs": [],
248+
"source": [
249+
"img = make_image()\n",
250+
"img_ref = weakref.ref(img)"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": null,
256+
"id": "f89da335-3372-486b-b773-9f103d6a9bbd",
257+
"metadata": {},
258+
"outputs": [],
259+
"source": [
260+
"img_ref()"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": null,
266+
"id": "c22904ad-d674-43e6-83bb-7a2f7b277c06",
267+
"metadata": {},
268+
"outputs": [],
269+
"source": [
270+
"del img"
271+
]
272+
},
273+
{
274+
"cell_type": "code",
275+
"execution_count": null,
276+
"id": "573566d7-eb91-4690-958c-d00dd495b3e4",
277+
"metadata": {},
278+
"outputs": [],
279+
"source": [
280+
"import gc"
281+
]
282+
},
283+
{
284+
"cell_type": "code",
285+
"execution_count": null,
286+
"id": "aaef3e89-2bfd-43af-9b8f-824a3f89b85f",
287+
"metadata": {},
288+
"outputs": [],
289+
"source": [
290+
"img_ref()"
291+
]
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": null,
296+
"id": "3380f35e-fcc9-43f6-80d2-7e9348cd13b4",
297+
"metadata": {},
298+
"outputs": [],
299+
"source": [
300+
"draw()"
301+
]
302+
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": null,
306+
"id": "a27bf7c7-f3ef-4ae8-8ecf-31507f8c0449",
307+
"metadata": {},
308+
"outputs": [],
309+
"source": [
310+
"print(\n",
311+
" subprocess.check_output([\"nvidia-smi\", \"--format=csv\", \"--query-gpu=memory.used\"]).decode().split(\"\\n\")[1]\n",
312+
" )"
313+
]
314+
},
315+
{
316+
"cell_type": "code",
317+
"execution_count": null,
318+
"id": "e4bf2711-8a83-4d9c-a4f7-f50de7ae1715",
319+
"metadata": {},
320+
"outputs": [],
321+
"source": []
322+
}
323+
],
324+
"metadata": {
325+
"kernelspec": {
326+
"display_name": "Python 3 (ipykernel)",
327+
"language": "python",
328+
"name": "python3"
329+
},
330+
"language_info": {
331+
"codemirror_mode": {
332+
"name": "ipython",
333+
"version": 3
334+
},
335+
"file_extension": ".py",
336+
"mimetype": "text/x-python",
337+
"name": "python",
338+
"nbconvert_exporter": "python",
339+
"pygments_lexer": "ipython3",
340+
"version": "3.10.5"
341+
}
342+
},
343+
"nbformat": 4,
344+
"nbformat_minor": 5
345+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.