php8.5
Home/ Manual/ luasandbox / luasandbox/ LuaSandbox::setMemoryLimit

LuaSandbox::setMemoryLimit

PHP function Edit on GitHub ✎

(PECL luasandbox >= 1.0.0)

Set the memory limit for the Lua environment

Description

LuaSandbox::setMemoryLimit(int $limit): void

Sets the memory limit for the Lua environment.

If this limit is exceeded, a LuaSandboxMemoryError exception is thrown.

Parameters

limit

Memory limit in bytes.

Return Values

Void

Examples

Calling a Lua function

php
<?php

// create a new LuaSandbox
$sandbox = new LuaSandbox();

// set a memory limit
$sandbox->setMemoryLimit( 50 * 1024 * 1024 );

// Run Lua code
$sandbox->loadString( 'local x = "x"; while true do x = x .. x; end' )->call();

?>

The above example will output something similar to:

output
PHP Fatal error:  Uncaught LuaSandboxMemoryError: not enough memory

See Also

  • LuaSandbox::getMemoryUsage
  • LuaSandbox::getPeakMemoryUsage
  • LuaSandbox::setCPULimit

Source: reference/luasandbox/luasandbox/setmemorylimit.xml · from the official PHP manual (php/doc-en)