現在の言語: 日本語 |
nullとunsetについて |
メモリ解放関連 |
| 項目 | unset() | $data = null; | 目的 | 指定した変数の割当を解除します。 シンボルテーブルから削除します。 | 変数にnullという特殊な値を代入します。 変数は存在を継続しています。 | メモリ解放 | 変数が参照されなくなるとガベージコレクションによって 非同期的にメモリが解放されます。 | 変数が保持していたデータへの参照が直ちになくなります。 より早くメモリが解放される可能性があります。 | CPU負荷 | ガベージコレクションにま変えるため、 CPUサイクルは節約されます。 | データの上書きとメモリ解放の処理が発生します。 CPUサイクルを消費しやすくなります。 | 処理 | 変数自体が存在しなくなります。 以降、該当する変数にアクセスすると Undefined variableのNoticeが発生します。 | 変数を一時的に値がない状態にしたい場合 オブジェクトの参照カヌンとを減らして早期にメモリを解放したい場合 |
class test1
{
public $child;
public $name;
public function __construct($name) {
$this->name = $name;
echo "オブジェクト {$this->name} が作成されました\n";
}
// デストラクタ:オブジェクトがメモリから解放される直前に呼ばれる
public function __destruct() {
echo "オブジェクト {$this->name} が破棄されました\n";
}
}
echo "<pre>";
$a = new test1("A");//オブジェクト A が作成されました
$b = new test1("B");//オブジェクト B が作成されました
// ----------------------------------------------------
// 循環参照を設定
// ----------------------------------------------------
$a->child = $b;
$b->child = $a;
// ----------------------------------------------------
//解放処理
// ----------------------------------------------------
$a = null;
echo "\$a を null に設定しました。\n";
//$aの参照が切断されます
//オブジェクトAのデストラクタが呼ばれ、メモリの解放が始まります。
unset($b);
echo "\$b を unset しました。\n";
//$bの参照が切断されます
//オブジェクトBのデストラクタが呼ばれ、メモリ解放が完了します。
echo "</pre>";
class test1
{
public $child;
public $name;
public function __construct($name)
{
$this->name = $name;
echo "Object {$this->name} was created\n";
}
/ Destructor: Called just before an object is released from memory.
public function __destruct()
{
echo "Object {$this->name} was destroyed\n";
}
}
echo "<pre>";
$a = new test1("A"); // Object A was created.
$b = new test1("B"); // Object B was created.
// ----------------------------------------------------
// Establish a circular reference.
// ----------------------------------------------------
$a->child = $b;
$b->child = $a;
// ----------------------------------------------------
// Release process.
// ----------------------------------------------------
$a = null;
echo "\$a to null" \n";
//The reference to $a is broken.
//The destructor for object A is called, and memory release begins.
unset($b);
echo "\$b has been unset. \n";
//The reference to $b is broken.
//The destructor for object B is called, and memory release is complete.
echo "</pre>";
class test2
{
function test1()
{
$a = 100; // $a が 100 として存在します
$a++; // $a は 101 になります
echo $a; // 101 が出力されます
unset($a); // ここで $a は完全に消滅します
$a = null; // 新しく$aという変数をnullで宣言し直しています
}
function test2()
{
$a = 100; // $a が 100 として存在します
$a++; // $a は 101 になります
echo $a; // 101 が出力します
$a = null; // $aの参照が切断されます(変数としては残っています)
unset($a); // 変数を削除します
}
function test3(int &$data)
{
$data = null;
unset($data);
}
function test4(int &$data)
{
// 値をnullにする操作だけを行う
$data = null;//値を消しています
//test3のように、ここではunsetを実行しないようにします
//unset($data);
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->test1();
$cls2->test2();
$a = 100;
$cls2->test3($a);
$a = 101;
$cls2->test4($a);
unset($a);//ここで実行します
echo "</pre>";
class test2
{
function test1()
{
$a = 100; // $a exists as 100
$a++; // $a becomes 101
echo $a; // 101 is output
unset($a); // $a is completely destroyed here
$a = null; // A new variable called $a is declared as null
}
function test2()
{
$a = 100; // $a exists as 100
$a++; // $a becomes 101
echo $a; // 101 is output
$a = null; // The reference to $a is severed (it remains as a variable)
unset($a); // The variable is deleted
}
function test3(int &$data)
{
$data = null;
unset($data);
}
function test4(int &$data)
{
//Only nullify the value.
$data = null; //Delete the value.
//Unset is not executed here, as in test3.
//unset($data);
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->test1();
$cls2->test2();
$a = 100;
$cls2->test3($a);
$a = 101;
$cls2->test4($a);
unset($a); //Execute here.
echo "</pre>";
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |