orlandothoeny / memory-limiter
PHP library that can read the available memory and set limit to currently free memory
1.0.0
2024-08-09 20:50 UTC
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.62
- phpstan/phpstan: ^1.11
- phpstan/phpstan-phpunit: ^1.4
- phpunit/phpunit: ^11.3
README
Memory Limiter is a PHP library that contains functionality to read the currently available/free memory of the system and to set the PHP memory limit according to the available memory.
Supports the following environments:
- Bare Metal Linux
- VM Linux
- Kubernetes Linux container
- Linux container (Docker, Podman, etc.)
Installation
composer require orlandothoeny/memory-limiter
Usage
Get currently available/free memory
<?php use MemoryLimiter\AvailableMemoryReader; $availableMemoryReader = AvailableMemoryReader::create(); $availableMemory = $availableMemoryReader->determineAvailableMemoryBytes();
Set PHP memory limit to currently available/free memory
<?php use MemoryLimiter\MemoryLimiter; $memoryLimiter = MemoryLimiter::create(); /* Set memory limit to the currently available memory Will skip setting the memory limit if running inside a Kubernetes container */ $memoryLimiter->setMemoryLimitToCurrentlyAvailableMemory(); // Also set memory limit when running inside a Kubernetes container $memoryLimiter->setMemoryLimitToCurrentlyAvailableMemory(false); // Set memory limit to 50% of the currently available memory $memoryLimiter->setMemoryLimitToCurrentlyAvailableMemory( limitToPercentageOfAvailableMemory: 50 );
Acknowledgments
- Teleboy: Sponsored initial development
Releases
See the releases page for a list of all releases. Releases are documented in the CHANGELOG.
This project uses semantic versioning as its versioning scheme.
Development
Install pre-commit hook
rm -f .git/hooks/pre-commit cp dev-environment/pre-commit.sh .git/hooks/pre-commit
Run locally
Prerequisites:
Start the development environment and SSH into the container
make quickstart
Separate commands
Build the container image
docker compose build
Run the container in the background
docker compose up -d
SSH into the container
Available commands:
- php
- composer
docker compose exec php sh
Stop the container
docker compose down
Tests
docker compose exec php composer test
PHPStan
docker compose exec php composer phpstan
Code Style
docker compose exec php composer cs-fix