Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
メモリプロファイラMemrayのススメ
Search
Manabu TERADA
October 28, 2023
Technology
3
1.2k
メモリプロファイラMemrayのススメ
PyCon APAC 2023 Day 2 Talk
Manabu TERADA (寺田 学)
2023-10-28
Manabu TERADA
October 28, 2023
Tweet
Share
More Decks by Manabu TERADA
See All by Manabu TERADA
Python実践試験、 Pythonデータ分析実践試験に 合格できるひとのレベル紹介と学習法
terapyon
0
93
各種大学向けにSSO対応サイト(SP)構築の事例紹介
terapyon
0
14
コンテンツタイプとUMLで考えるPlone開発(1)
terapyon
0
56
Python Web UIフレームワークのススメ
terapyon
0
1k
LLMを用いたPloneベクター検索 アドオンの開発の説明
terapyon
0
91
PyCon APACの軌跡
terapyon
0
1.9k
Pythonエンジニアになるためのテクニカルハイライトと学習方法
terapyon
0
110
Multi lingual Vector search using LLM
terapyon
1
91
PoC for LLM search on Plone
terapyon
1
110
Other Decks in Technology
See All in Technology
2024.02.19 W&B AIエージェントLT会 / AIエージェントが業務を代行するための計画と実行 / Algomatic 宮脇
smiyawaki0820
13
3.5k
関東Kaggler会LT: 人狼コンペとLLM量子化について
nejumi
3
590
利用終了したドメイン名の最強終活〜観測環境を育てて、分析・供養している件〜 / The Ultimate End-of-Life Preparation for Discontinued Domain Names
nttcom
2
200
分解して理解する Aspire
nenonaninu
1
200
Goで作って学ぶWebSocket
ryuichi1208
1
1.2k
「海外登壇」という 選択肢を与えるために 〜Gophers EX
logica0419
0
710
転生CISOサバイバル・ガイド / CISO Career Transition Survival Guide
kanny
3
1k
CZII - CryoET Object Identification 参加振り返り・解法共有
tattaka
0
370
PHPカンファレンス名古屋-テックリードの経験から学んだ設計の教訓
hayatokudou
2
320
全文検索+セマンティックランカー+LLMの自然文検索サ−ビスで得られた知見
segavvy
2
110
OpenID BizDay#17 KYC WG活動報告(法人) / 20250219-BizDay17-KYC-legalidentity
oidfj
0
250
目の前の仕事と向き合うことで成長できる - 仕事とスキルを広げる / Every little bit counts
soudai
24
7.2k
Featured
See All Featured
Navigating Team Friction
lara
183
15k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
174
51k
KATA
mclloyd
29
14k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.6k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
GitHub's CSS Performance
jonrohan
1030
460k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Site-Speed That Sticks
csswizardry
4
380
Git: the NoSQL Database
bkeepers
PRO
427
64k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Transcript
copyright © 2023 CMS Comunications Inc. all rights reserved. メモリプロファイラ
Memray のススメ ─Pythonのメモリプロファイラの紹介─ 株式会社CMSコミュニケーションズ 代表取締役 寺田 学 (Manabu TERADA) 「PyCon APAC 2023」 2023年10月28日
copyright © 2023 CMS Comunications Inc. all rights reserved. 記事が公開されている:
今日はデモ多め 文章 で読みたい方 gihyo.jp Python Monthly Topics 2023年6月 メモリプロファイラ「Memray」の解説 https://gihyo.jp/article/2023/06/monthly-python-2306
copyright © 2023 CMS Comunications Inc. all rights reserved. 講演の流れ
• 自己紹介 • メモリプロファイラとは • Python用メモリプロファイラMemrayの紹介 • Memrayの基本的な使い方 • Pythonのコーディングによる違い • NumPyを使った確認 • 一歩進んだ使い方
copyright © 2023 CMS Comunications Inc. all rights reserved. 自己紹介
copyright © 2023 CMS Comunications Inc. all rights reserved. 自己紹介
寺田 学 (Manabu TERADA) • Pythonエンジニア • Podcast 「terapyon channel」(https://podcast.terapyon.net) • 主な関連書籍(共著・監修・監訳) ◦ Pythonデータ分析 実践ハンドブック (インプレス: 2023年9月) ◦ Pythonによるあたらしいデータ分析の教科書第 2版(翔泳社:2022年10月) ◦ Python実践レシピ (技術評論社: 2022年1月) ◦ スラスラわかるPython第2版(翔泳社:2021年11月) ◦ 機械学習図鑑(翔泳社 : 2019年4月)
copyright © 2023 CMS Comunications Inc. all rights reserved. 主な役職
寺田 学 (Manabu TERADA) 株式会社 CMSコミュニケーションズ 代表取締役 • Pythonエンジニア • 一般社団法人PyCon JP Association 理事 • 一般社団法人Pythonエンジニア育成推進協会顧問理事 • Python Software Foundation (PSF) Fellow • 国立大学法人一橋大学 社会学研究科 元客員准教授(2022)
copyright © 2023 CMS Comunications Inc. all rights reserved. Python技術支援のコンサルサービス
(株)CMSコミュニケーションズのサービス
copyright © 2023 CMS Comunications Inc. all rights reserved. メモリプロファイラとは
• 必要性 • 2つの手法を紹介 • ツールの紹介 • プロファイリングの難しさ
copyright © 2023 CMS Comunications Inc. all rights reserved. メモリプロファイラの必要性
メモリ状態を管理していますか? • 実行にどのくらいメモリを使うのか知りたい • 実行が止まるが、どこでメモリを使っているか知りたい • コードやライブラリによるメモリ使用量の違いを知りたい • 大規模データを実メモリ内で処理できるデータ量を知りたい
copyright © 2023 CMS Comunications Inc. all rights reserved. 2つの手法を紹介
トレーシング(tracing ) • すべての状況を知る • メモリ確保・開放に ◦ 追加処理を入れる • 実行速度への影響 サンプリング(sampling ) • 定期的にデータ取得 • OSに問い合わせる • 統計的手法 or
copyright © 2023 CMS Comunications Inc. all rights reserved. 手法ごとのツールの紹介
トレーシング(tracing ) • Memray ◦ このトークのメイン サンプリング(sampling ) • psutil ◦ ベーシックなツール • memory-profiler ◦ メンテナンス終了 ◦ psutilに依存
copyright © 2023 CMS Comunications Inc. all rights reserved. プロファイリングの難しさ
• 統計的なデータ取得で良いのか? • ピンポイントの状況把握が必要なときどうする? • トレーシングで、実行負荷や実行速度に影響ない? • 実際にどのオブジェクトがメモリを使っているか知りたい ◦ サンプリング手法では完全把握できない
copyright © 2023 CMS Comunications Inc. all rights reserved. Python用メモリプロファイラ
Memrayの紹介 • MemrayはPyCon US 2022でお披露目 • Memrayの概要 • Memrayのインストール方法
copyright © 2023 CMS Comunications Inc. all rights reserved. Memray!!
copyright © 2023 CMS Comunications Inc. all rights reserved. MemrayはPyCon
US 2022 LTでお披露目 https://youtu.be/1IiL31tUEVk?si=JoAkPTdvkw30Avf1&t=951
copyright © 2023 CMS Comunications Inc. all rights reserved. MemrayとPyCon
US 2023で再会 https://youtu.be/mqu66lg79X8
copyright © 2023 CMS Comunications Inc. all rights reserved. Memrayの概要
• トレーシング手法を採用 • LinuxとmacOSに対応 (Windowsには対応していない) ライブラリ名 memray Pythonバージョン Python 3.8以降 公式サイト https://bloomberg.github.io/memray/index.html PyPI https://pypi.org/project/memray/ GitHub https://github.com/bloomberg/memray 現在のバージョン 1.10.0
copyright © 2023 CMS Comunications Inc. all rights reserved. Memrayの歴史・開発者
• ブルームバーグの社内ツールとして開発スタート • 2022年4月にOSS化 • Pablo Galindo Salgado氏が開発をリード ◦ Pythonのコアデベロッパー ◦ CPythonのリリースマネジャー ◦ 現在 Council メンバーでもある
copyright © 2023 CMS Comunications Inc. all rights reserved. インストール方法
$ pip install memray memrayコマンドが使えるようになる
copyright © 2023 CMS Comunications Inc. all rights reserved. Memrayの基本的な使い方
• プロファイルの実行 • 結果をHTMLで表示 • 現状をライブで見る
copyright © 2023 CMS Comunications Inc. all rights reserved. プロファイルの実行方法
$ memray run モジュール sample.py のプロファイリングをする場合 $ memray run sample.py memray-...bin というファイルが出力
copyright © 2023 CMS Comunications Inc. all rights reserved. 結果をHTMLで表示
$ memray flamegraph 結果ファイル名 runで実行した時に出力れた結果ファイル memray-...bin を使って結果をHTML化 $ memray flamegraph memray-...bin memray-flamegraph-sample.py.xxxx.html というファイルが出力
copyright © 2023 CMS Comunications Inc. all rights reserved. 結果をHTMLで表示
(画面キャプチャ)
copyright © 2023 CMS Comunications Inc. all rights reserved. 結果をHTMLで表示
(グラフポップアップ)
copyright © 2023 CMS Comunications Inc. all rights reserved. プロファイルの実行と結果をHTMLで表示
デモ
copyright © 2023 CMS Comunications Inc. all rights reserved. 現状をライブで見る
$ memray run --live モジュール コンソールに実行時の状況がライブで表示 $ memray run --live sample.py 別コンソールに出力する --live-remote モードもある リモートモードは表示確認を始めないと、スクリプトが動かない
copyright © 2023 CMS Comunications Inc. all rights reserved. 現状をライブで見る
(画面キャプチャ)
copyright © 2023 CMS Comunications Inc. all rights reserved. 現状をライブで見る
デモ
copyright © 2023 CMS Comunications Inc. all rights reserved. Pythonのコーディングによる
違い • メモリの確保状況の違い(リストオブジェクトの状況) • 実行時間の違いも見れる
copyright © 2023 CMS Comunications Inc. all rights reserved. メモリの確保状況の違い(リストオブジェクトの状況)
import time SIZE = 1024 * 1024 * 1024 # 1G time.sleep(1) # リスト内包表記でリストを生成 result = [1 for _ in range(SIZE)] time.sleep(1) del result # 変数を削除 time.sleep(1) sample-list.py を準備 result2 = [] # for文とappendメソッドでリストを生成 for i in range(SIZE): result2.append(1) time.sleep(1) del result2 # 変数を削除 time.sleep(1) result3 = [1] * SIZE # 掛け算でリストを生成 time.sleep(1) del result3 # 変数を削除 time.sleep(1)
copyright © 2023 CMS Comunications Inc. all rights reserved. メモリの確保状況の違い(リストオブジェクトの状況)
$ memray run sample-list.py 結果ファイルのファイル名を記録しHTML出力 $ memray flamegraph memray-...bin
copyright © 2023 CMS Comunications Inc. all rights reserved. メモリの確保状況の違い
画面キャプチャ 実行時間の違いも一目瞭然
copyright © 2023 CMS Comunications Inc. all rights reserved. NumPyを使った確認
• メモリ確保の可視化 • C拡張でも状況がきちんとわかる
copyright © 2023 CMS Comunications Inc. all rights reserved. NumPyのメモリ確保の可視化
import time import numpy as np SIZE = 1024 * 1024 * 1024 # 1G time.sleep(1) # 要素数が約1億個のNumPy配列を # 8bit整数型で生成 arr = np.ones(SIZE, dtype=np.uint8) time.sleep(1) # 同じ変数名に同じ配列を再代入 arr = np.ones(SIZE, dtype=np.uint8) sample-arr.py を準備 time.sleep(1) # 変数を削除 del arr time.sleep(1) arr = np.ones(SIZE, dtype=np.uint8) time.sleep(1) # 別の変数に代入 arr2 = np.ones(SIZE, dtype=np.uint8) time.sleep(1)
copyright © 2023 CMS Comunications Inc. all rights reserved. NumPyのメモリ確保の可視化
$ memray run sample-arr.py 結果ファイルのファイル名を記録しHTML出力 $ memray flamegraph memray-...bin
copyright © 2023 CMS Comunications Inc. all rights reserved. NumPyのメモリ確保の可視化
画面キャプチャ 変数上書きの瞬間、 2倍のメモリ量を要求している
copyright © 2023 CMS Comunications Inc. all rights reserved. C拡張の状況
$ memray run --native sample-arr.py --nativeモードを使う $ memray flamegraph memray-...bin 詳細の情報が表示される
copyright © 2023 CMS Comunications Inc. all rights reserved. C拡張の状況
画面キャプチャ
copyright © 2023 CMS Comunications Inc. all rights reserved. NumPyオブジェクトをnativeモード
デモ
copyright © 2023 CMS Comunications Inc. all rights reserved. 一歩進んだ使い方
• Jupyter Integration を使う • with ステートメントで部分的に状態を見る • pytest limit でテストにメモリ制限を設ける
copyright © 2023 CMS Comunications Inc. all rights reserved. Jupyter
Integration を使う %load_ext memray マジックコマンドでモジュールのロード %%memray_flamegraph セルマジックを使ってセル内の実行状況確認
copyright © 2023 CMS Comunications Inc. all rights reserved. Jupyter
Integration を使う デモ
copyright © 2023 CMS Comunications Inc. all rights reserved. with
ステートメントで部分的に状態を見る with Tracker("memray-profile.bin"): トレーシングしたい部分をwithブロックにする $ memray flamegraph memray-profile.bin flamegraphで結果をHTML化
copyright © 2023 CMS Comunications Inc. all rights reserved. pytest
limit でテストにメモリ制限を設ける $ pip install pytest-memray pytest拡張をインストール @pytest.mark.limit_memory("24 MB") def test_foobar(): pass
copyright © 2023 CMS Comunications Inc. all rights reserved. まとめ
copyright © 2023 CMS Comunications Inc. all rights reserved. まとめ
• メモリプロファイリングをやってみよう。 • コードによる違いを確認しよう。 • メモリ不足への対処に強くなろう。
copyright © 2023 CMS Comunications Inc. all rights reserved. ご清聴
ありがとうございました。
copyright © 2023 CMS Comunications Inc. all rights reserved. Q&A