Skip to content

PHP library that can read the available memory and set limit to currently free memory

License

Notifications You must be signed in to change notification settings

orlandothoeny/memory-limiter

Repository files navigation

Memory Limiter

Latest Stable Version PHP Version codecov PHPStan level Dependencies License Downloads

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

About

PHP library that can read the available memory and set limit to currently free memory

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages