wzul

PHP Code to test memory limit

Dec 10th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. <?php
  2. // Display the current memory limit
  3. echo "Current memory limit: " . ini_get('memory_limit') . "\n";
  4.  
  5. // Function to allocate memory
  6. function allocateMemory($sizeInMB) {
  7.     $sizeInBytes = $sizeInMB * 1024 * 1024;
  8.     $memory = str_repeat('a', $sizeInBytes);
  9.     return $memory;
  10. }
  11.  
  12. try {
  13.     $allocatedMemory = [];
  14.     $i = 1;
  15.     while (true) {
  16.         echo "Allocating {$i}MB of memory\n";
  17.         $allocatedMemory[] = allocateMemory(1); // Allocate 1MB at a time
  18.         $i++;
  19.     }
  20. } catch (Exception $e) {
  21.     echo "Caught exception: " . $e->getMessage() . "\n";
  22. } catch (Error $e) {
  23.     echo "Caught error: " . $e->getMessage() . "\n";
  24. }
  25. ?>
  26.  
Advertisement
Add Comment
Please, Sign In to add comment