2



Need advise here. Currently I am get the memory usage of the script by using this code

$sysMem = escapeshellcmd(system('echo $(free)'));
memory_get_usage();

This is the result i am getting :

Total Mem : 1034708
Used Mem : 1014572
Free Mem : 20136
Shared Mem : 0
Buff Mem : 73456
Cached Mem : 480752
----------------------------------------------------
Mem Usegae : 509464

Total Mem : 1034708
Used Mem : 1014564
Free Mem : 20144
Shared Mem : 0
Buff Mem : 73456
Cached Mem : 480828
----------------------------------------------------
Mem Usegae : 343904

However i found out that the memory usage is kind of inconsistent and at time the memory usage might even exceed the total memory which is impossible.

Is memory_get_usage(); the best option to get the memory usage of the php script? Or is it i need to use unset() function. However even if i use the memory still about the same

if there are other methods please kindly advise.

Thanks a millions
Guys and Ladies

1
  • It's not impossible to have ram usage exceed physical ram - there's always virtual memory. Commented Sep 1, 2011 at 16:35

2 Answers 2

1

Maybe this will help using pure PHP. You can get the exact total memory using:

ini_get('memory_limit');

But this will be something like '128M'. To convert this form into bytes look at Stack-Overflow-Convert String Mem-Bytes into bytes. Now you have 2 methods for getting the script memory usage:

memory_get_peak_usage();

and

memory_get_usage();

With these ingedients the puzzle is easy (imho).

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

2 Comments

i don't see how ini_get.. is helpful here, it only says up to how much the script can get at max!
I agree with you @Dany Khalife
0

This is going to be difficult to state precisely. You might need to specify more carefully exactly what you are trying to do.

For one, calling out to the 'free' command is stepping off into the void of Linux's virtual memory scheme. You need to know how to interpret these meaningfully.

As far as I can tell, get_memory_usage could behave in a number of different ways depending on how PHP is running. It appears to be returning how much memory is allocated to the current PHP interpreter in total. This is unlikely to be indicative of how much memory the current script is using.

Also, free returns memory in kilobytes. PHP's get_memory_usage returns it in bytes. You are off by a factor of 1024. Divide the latter by 1024 to have comparable units.

So what are you trying to do?

2 Comments

what i am actually trying to do is to optimize my script to run. I am currently using curl function in php for multi threading which is running all day. However at a certain time it die off. so i was thinking of getting the memory usage of the script and then get the system memory usage available to do some calculation to check if there is enough memory to proceed with script or to sleep the script.
Sleeping the script is not going to free memory. The PHP interpreter is going to hold onto that memory already allocated unless the script actually terminates.

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.