PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 1.9.6
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v1.9.6
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / class-MemoryLeakDetector.php

class-MemoryLeakDetector.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 1.9.6, at inc/class-MemoryLeakDetector.php

60 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class MemoryLeakDetector
4 {
5 private $checkpoints = [];
6 private $threshold;
7 private $logFile;
8
9 public function __construct($threshold = 1024 * 1024, $logFile = null) // Default threshold is 1MB
10 {
11 $this->threshold = $threshold;
12 $this->logFile = $logFile ?: optifer_PATH . 'memory_leaks.log';
13 }
14
15 public function addCheckpoint($name)
16 {
17 $this->checkpoints[$name] = memory_get_usage();
18 }
19
20 public function detectLeaks()
21 {
22 $previousUsage = null;
23 $leaks = [];
24
25 foreach ($this->checkpoints as $name => $usage) {
26 if ($previousUsage !== null && ($usage - $previousUsage) > $this->threshold) {
27 $leaks[] = [
28 'checkpoint' => $name,
29 'increase' => $usage - $previousUsage
30 ];
31 }
32 $previousUsage = $usage;
33 }
34
35 return $leaks;
36 }
37
38 public function reportLeaks()
39 {
40 $leaks = $this->detectLeaks();
41
42 if (empty($leaks)) {
43 $this->log("No memory leaks detected.\n");
44 } else {
45 $this->log("Potential memory leaks detected:\n");
46 foreach ($leaks as $leak) {
47 $this->log("Checkpoint: {$leak['checkpoint']}, Memory increase: {$leak['increase']} bytes\n");
48 }
49 }
50 }
51
52 private function log($message)
53 {
54 file_put_contents($this->logFile, $message, FILE_APPEND);
55 }
56 }
57
58
59 global $mem_detector;
60 $mem_detector = new MemoryLeakDetector(1.5 * 1024 * 1024); // 1.5MB threshold