PHP code example of yousefkadah / laravel-memory-profiler

1. Go to this page and download the library: Download yousefkadah/laravel-memory-profiler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yousefkadah / laravel-memory-profiler example snippets


return [
    // Output directory for reports
    'output_directory' => storage_path('memory-profiles'),
    
    // Sampling interval in milliseconds
    'sampling_interval' => 100,
    
    // Memory threshold for warnings (in bytes)
    'memory_threshold' => 128 * 1024 * 1024, // 128MB
    
    // Report format: 'json', 'html', or 'both'
    'report_format' => 'both',
    
    // Track database queries
    'track_database_queries' => true,
    
    // Track garbage collection
    'track_garbage_collection' => true,
    
    // Auto-cleanup old reports (days)
    'auto_cleanup_days' => 30,
];

// ❌ Problematic: Loading all records at once
$users = User::all();
foreach ($users as $user) {
    // Process user
}

// ✅ Better: Use chunking
User::chunk(100, function ($users) {
    foreach ($users as $user) {
        // Process user
    }
});

// ❌ Problematic: Query log grows indefinitely
DB::enableQueryLog();
// ... many queries ...

// ✅ Better: Disable or clear query log
DB::disableQueryLog();
// or
DB::flushQueryLog();

// ❌ Problematic: Static arrays that grow
class MyService {
    public static $cache = [];
    
    public function process($item) {
        self::$cache[] = $item; // Grows forever
    }
}

// ✅ Better: Clear static variables periodically
MyService::$cache = [];
bash
php artisan vendor:publish --tag=memory-profiler-config