211 Timings::$memoryManager->startTiming();
213 if(($this->memoryLimit > 0 || $this->globalMemoryLimit > 0) && ++$this->checkTicker >= $this->checkRate){
214 $this->checkTicker = 0;
215 $memory = Process::getAdvancedMemoryUsage();
217 if($this->memoryLimit > 0 && $memory[0] > $this->memoryLimit){
219 }elseif($this->globalMemoryLimit > 0 && $memory[1] > $this->globalMemoryLimit){
223 if($trigger !==
false){
224 if($this->lowMemory && $this->continuousTrigger){
225 if(++$this->continuousTriggerTicker >= $this->continuousTriggerRate){
226 $this->continuousTriggerTicker = 0;
227 $this->trigger($memory[$trigger], $this->memoryLimit, $trigger > 0, ++$this->continuousTriggerCount);
230 $this->lowMemory =
true;
231 $this->continuousTriggerCount = 0;
232 $this->trigger($memory[$trigger], $this->memoryLimit, $trigger > 0);
235 $this->lowMemory =
false;
239 if($this->garbageCollectionPeriod > 0 && ++$this->garbageCollectionTicker >= $this->garbageCollectionPeriod){
240 $this->garbageCollectionTicker = 0;
241 $this->triggerGarbageCollector();
244 Timings::$memoryManager->stopTiming();
287 public static function dumpMemory(mixed $startingObject,
string $outputFolder,
int $maxNesting,
int $maxStringSize, \
Logger $logger) : void{
288 $hardLimit =
Utils::assumeNotFalse(ini_get(
'memory_limit'),
"memory_limit INI directive should always exist");
289 ini_set(
'memory_limit',
'-1');
292 if(!file_exists($outputFolder)){
293 mkdir($outputFolder, 0777,
true);
296 $obData = Utils::assumeNotFalse(fopen(Path::join($outputFolder,
"objects.js"),
"wb+"));
302 $instanceCounts = [];
304 $staticProperties = [];
307 $functionStaticVars = [];
308 $functionStaticVarsCount = 0;
310 foreach(get_declared_classes() as $className){
311 $reflection = new \ReflectionClass($className);
312 $staticProperties[$className] = [];
313 foreach($reflection->getProperties() as $property){
314 if(!$property->isStatic() || $property->getDeclaringClass()->getName() !== $className){
318 if(!$property->isInitialized()){
323 $staticProperties[$className][$property->getName()] = self::continueDump($property->getValue(), $objects, $refCounts, 0, $maxNesting, $maxStringSize);
326 if(count($staticProperties[$className]) === 0){
327 unset($staticProperties[$className]);
330 foreach($reflection->getMethods() as $method){
331 if($method->getDeclaringClass()->getName() !== $reflection->getName()){
335 foreach(Utils::promoteKeys($method->getStaticVariables()) as $name => $variable){
336 $methodStatics[$name] = self::continueDump($variable, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
338 if(count($methodStatics) > 0){
339 $functionStaticVars[$className .
"::" . $method->getName()] = $methodStatics;
340 $functionStaticVarsCount += count($functionStaticVars);
345 file_put_contents(Path::join($outputFolder,
"staticProperties.js"), json_encode($staticProperties, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
346 $logger->
info(
"Wrote $staticCount static properties");
348 $globalVariables = [];
363 foreach(Utils::promoteKeys($GLOBALS) as $varName => $value){
364 if(isset($ignoredGlobals[$varName])){
369 $globalVariables[$varName] = self::continueDump($value, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
372 file_put_contents(Path::join($outputFolder,
"globalVariables.js"), json_encode($globalVariables, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
373 $logger->
info(
"Wrote $globalCount global variables");
375 foreach(get_defined_functions()[
"user"] as $function){
376 $reflect = new \ReflectionFunction($function);
379 foreach(Utils::promoteKeys($reflect->getStaticVariables()) as $varName => $variable){
380 $vars[$varName] = self::continueDump($variable, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
382 if(count($vars) > 0){
383 $functionStaticVars[$function] = $vars;
384 $functionStaticVarsCount += count($vars);
387 file_put_contents(Path::join($outputFolder,
'functionStaticVars.js'), json_encode($functionStaticVars, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
388 $logger->
info(
"Wrote $functionStaticVarsCount function static variables");
390 $data = self::continueDump($startingObject, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
394 foreach(Utils::stringifyKeys($objects) as $hash => $object){
395 if(!is_object($object)){
400 $className = get_class($object);
401 if(!isset($instanceCounts[$className])){
402 $instanceCounts[$className] = 1;
404 $instanceCounts[$className]++;
407 $objects[$hash] =
true;
409 "information" =>
"$hash@$className",
411 if($object instanceof \Closure){
412 $info[
"definition"] = Utils::getNiceClosureName($object);
413 $info[
"referencedVars"] = [];
414 $reflect = new \ReflectionFunction($object);
415 if(($closureThis = $reflect->getClosureThis()) !==
null){
416 $info[
"this"] = self::continueDump($closureThis, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
419 foreach(Utils::promoteKeys($reflect->getStaticVariables()) as $name => $variable){
420 $info[
"referencedVars"][$name] = self::continueDump($variable, $objects, $refCounts, 0, $maxNesting, $maxStringSize);
423 $reflection = new \ReflectionObject($object);
425 $info[
"properties"] = [];
427 for($original = $reflection; $reflection !==
false; $reflection = $reflection->getParentClass()){
428 foreach($reflection->getProperties() as $property){
429 if($property->isStatic()){
433 $name = $property->getName();
434 if($reflection !== $original){
435 if($property->isPrivate()){
436 $name = $reflection->getName() .
":" . $name;
441 if(!$property->isInitialized($object)){
445 $info[
"properties"][$name] = self::continueDump($property->getValue($object), $objects, $refCounts, 0, $maxNesting, $maxStringSize);
450 fwrite($obData, json_encode($info, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) .
"\n");
455 $logger->
info(
"Wrote " . count($objects) .
" objects");
459 file_put_contents(Path::join($outputFolder,
"serverEntry.js"), json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
460 file_put_contents(Path::join($outputFolder,
"referenceCounts.js"), json_encode($refCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
462 arsort($instanceCounts, SORT_NUMERIC);
463 file_put_contents(Path::join($outputFolder,
"instanceCounts.js"), json_encode($instanceCounts, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
465 $logger->
info(
"Finished!");
467 ini_set(
'memory_limit', $hardLimit);