Skip to content

💾 SetMemoryLimit

Sets the PHP memory limit. Equivalent to setting the COMPOSER_MEMORY_LIMIT environment variable.

When to use

Use this when Composer reports a PHP out-of-memory error (e.g. Allowed memory size of ... exhausted), commonly seen when running install/update on large projects.

Signature

go
func SetMemoryLimit(limit string) error

Parameters

ParameterTypeDescription
limitstringMemory limit value, in the same format as PHP ini, e.g. 512M, 2G, -1 (unlimited)

Return value

Return valueTypeDescription
Sole return valueerrorReturned when the setting fails (usually nil)

Example

go
package main

import (
	"fmt"
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	if err := composer.SetMemoryLimit("2G"); err != nil {
		log.Fatalf("set memory limit failed: %v", err)
	}
	fmt.Println("Composer memory limit set to 2G")
}

Advanced

  • This function sets EnvComposerMemoryLimit under the hood; the same effect can be achieved with SetEnvVariable.
  • To read the current value, use GetEnvVariable(composer.EnvComposerMemoryLimit).
  • In containers/CI, it is recommended to set this before startup so it applies to all subsequent Composer invocations.

Released under the MIT License