I am trying to avoid memory leak in PHP. When I create an object and disconnect it at the end, it is still in memory. The reset is as follows:
$obj = NULL; unset($obj);
However, this does not help.
My question is what will happen when I redo the proccess and the object will be created and destroyed in the child stream? Will it be the same? Or is there another way to free memory?
This is an import script that will consume several gigabytes.
In PHP 5.3 there is a garbage collector that can collect circular references. It might be worth a try:
gc_enable(); class A { public function __construct() { $this->data = str_repeat("A", 1024000); } } $mem = memory_get_usage(); $a = new A(); $mem2 = memory_get_usage(); $a->a = $a; $a->a->mydata = $a->data . 'test'; $mem3 = memory_get_usage(); unset($a); gc_collect_cycles(); $mem4 = memory_get_usage(); printf("MEM 1 at start %0.2f Mb\n", ($mem / 1024) / 1024); printf("MEM 2 after first instantiation %0.2f Mb\n", ($mem2 / 1024) / 1024); printf("MEM 3 after self-ref: %0.2f Mb\n", ($mem3 / 1024) / 1024); printf("MEM 4 after unset(\$a): %0.2f Mb\n", ($mem4 / 1024) / 1024);
Output:
MEM 1 at start: 0.31 Mb MEM 2 after first instantiation: 1.29 Mb MEM 3 after self-ref: 2.26 Mb MEM 4 after unset($a): 0.31 Mb
NULL unset(). , , unset() . , , , , , , .
unset()
, script . ( ), . , , , .
, , , , . MathieuK, PHP5.3 gc *, . PHP - , , ( , , -, "" script..).
Source: https://habr.com/ru/post/1716765/More articles:How to do this using the java expression language - javaUsing Curry to Define Grails Tags - closuresProblems saving a previously saved object in Google App Engine (Java) - javaPHP object class variable - oopJavascript RTE with spellchecker - javascriptMove items in the array of strings to the left to fill in the βholesβ - arraysWhy don't we glue the private key in an asymmetric cryptographic system? - securityScala Listing and ReadingResolve - enumsCreating your own FILE * pointer in C? - cCharacter Encoding Problem - Strange Pound (Β£) Signs with UTF-8 IE6 / ASP / XML - utf-8All Articles