1

I can use both ini_set('memory_limit', '512M'); in the file and php -d memory_limit=512M from the command line, but can also trace memory usage from terminal?

I know I can use memory_get_usage() inside a PHP file, but how to trace it from the command line?

3
  • 1
    php -r "var_dump(memory_get_usage());" Commented Aug 4, 2020 at 8:26
  • @Justinas If I'll add watch -n 1 I will see how usage is changing while during the runtime. Is there a better way to find out? Commented Aug 4, 2020 at 8:38
  • 1
    php -r "var_dump(memory_get_usage());" will not help you, because it Returns the amount of memory allocated to PHP. You need to pass true as parameter to see the real used memory. Commented Aug 4, 2020 at 9:40

2 Answers 2

1

Try:

$ watch -n 5 'php -r "var_dump(memory_get_usage());"'

This will watch every 5 seconds the memory state

Or may be you can use the 'ps' tool:

$ ps -F -C php-cgi

Output:

UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
http     10794 10786  0  4073   228   0 Jun09 ?        00:00:00 /usr/bin/php-cgi

RSS is the Real-memory (resident set) size in kilobytes of the process.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this solution is more robust one than mine
0

The solution I was looking for with a simple output is

watch -n 5 'php -r "echo (string) memory_get_usage(true)/pow(10, 6);"'

Will return how many MB the PHP process is using.

2.097152

NOTICE: Solution like ps -F -C php-cgi on macOS machines will fail with

ps: illegal option -- F

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.