OPcacheのキャッシュサイズを確認する
PHP OPcacheのキャッシュサイズを確認する方法はいくつかありますが、2つほどその方法をご紹介します。
PHPスクリプトで確認する
OPcacheの状態を確認するには、opcache_get_status() 関数を使用します。この関数はOPcacheのメモリ使用状況を含む詳細な情報を返しますので、この関数を利用して下記のようなphpスクリプトを作成して確認します
<?php
if (function_exists('opcache_get_status')) {
$status = opcache_get_status();
if ($status && isset($status['memory_usage'])) {
echo "Used memory: " . ($status['memory_usage']['used_memory'] / 1024 / 1024) . " MB\n";
echo "Free memory: " . ($status['memory_usage']['free_memory'] / 1024 / 1024) . " MB\n";
echo "Wasted memory: " . ($status['memory_usage']['wasted_memory'] / 1024 / 1024) . " MB\n";
} else {
echo "Failed to fetch OPcache status.";
}
} else {
echo "OPcache is not enabled.";
}
?>
【結果】
- Used memory: 使用中のメモリ。
- Free memory: 未使用のメモリ。
- Wasted memory: 無駄なメモリ(ガベージコレクション可能な部分)
作成したプログラムをWEBサーバーに配置して確認したり、CUI環境で実行します
$ php ./opcache_status.php Used memory: 64.744987487793 MB Free memory: 191.25501251221 MB Wasted memory: 0 MB
OPcache管理ツールを使用する
GitHubで公開されている opcache-gui などの管理ツールを使うと、キャッシュ状況を視覚的に確認できます。
【手順】
- opcache-guiをインストールします。
- Webサーバーに配置してブラウザで開きます。
- 詳細なキャッシュ状況をリアルタイムで確認できます。
CLIでphpコマンドで確認
「opcache_get_status」関数をphpコマンドを利用して確認する
$ php -r 'print_r(opcache_get_status());'
Array
(
[opcache_enabled] => 1
[cache_full] =>
[restart_pending] =>
[restart_in_progress] =>
[memory_usage] => Array
(
[used_memory] => 67888192
[free_memory] => 200547264
[wasted_memory] => 0
[current_wasted_percentage] => 0
)
# 以下は、省略
)



コメント