0
0

More than 3 years have passed since last update.

phpでuseした時ってメモリ使うんだっけ

Last updated at Posted at 2020-06-14

phpでuseした時ってメモリ使うんだっけ

わからんのでやってみよう。

やってみる

やった環境

$ php -v
PHP 7.4.6 (cli) (built: May 14 2020 10:38:36) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v2.9.2, Copyright (c) 2002-2020, by Derick Rethans
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

やってみた

main.php
<?php

namespace main;

$usage['current'] = memory_get_usage();    
logging($usage, 'initialize usage.');

require_once('empty.php');
logging($usage, 'require empty.php.');

require_once('hoge.php');

logging($usage, 'require hoge.php.');
\hoge\getName();

use hoge as piyo;
logging($usage, 'set alias.');
piyo\getName();

function logging(array &$usages, string $tag = ''): void {
    $usages['before'] = $usages['current'];
    $usages['current'] = memory_get_usage();
    echo sprintf('usage memory %4d. %s'.PHP_EOL, ($usages['current'] - $usages['before']), $tag);
}

な感じで↓を読み込んでみた

empty.php
<?php
hoge.php
<?php

namespace hoge;

function getName(): string
{
    return 'hoge';
}

やった結果

$ php main.php 
usage memory  408. initialize usage.
usage memory  224. require empty.php.
usage memory  528. require hoge.php.
usage memory    0. set alias.

requireしたときにメモリ消費して、useのときには消費しないと
名前空間ってむずかしい

おまけ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0