views:

39

answers:

4

I have a script when ran consumes 17mb of memory (logged using memory_get_peak_usage()).

Script is ran 1 million times per day. Total daily memory consumption: 17 million mb

86400 seconds in a day.

17000000 / 86400 = 196.76

Assumption: Running this script 1 million times each day will require at least 196.76 dedicated memory.

Is my assumption correct?

A: 

Yes, but what if it gets called multiple times at the same time? You have multiple threads running simultaneously.

EDIT: Also, why is that script running a million times / day? (unless you got a huge website)

David Weitz
A: 

I dont think this calculation will give correct results because you also need to consider other factors such as -

  1. How long the script runs.
  2. What are the number of scripts running at a time.
  3. Distribution of the invocation of scripts throughout the time.

And in your calculation whats the point in dividing by 86400 seconds? why not hours or milliseconds. To me, the calculation seems pretty meaningless.

Gopi
in response to your question I am trying to calculate what type of resources (memory) I will need to power a very large amount of requests at any given period of time (thus the reason why I used seconds)
+2  A: 

If script is runs 1000000 copies at the same time, then you would get 17 million MB, but as it releases memory after to completes, you don't add usage to total sum.

You need to know how much copies run at same time and multiply that number of 17 MB. That would be max memory usage.

Im0rtality
makes sense! I was looking at this problem entirely wrong.. it really only matters how many are concurrently downloading. I'll keep that in mind, thanks!
+1  A: 

Not entirely correct; the first hundred times your script is executed, it'll probably all fit into memory fine; so, the first two minutes or so might go as expected. But, once you push your computer into swap, your computer will spend so much time handling swap, that the next 999,800 executions might go significantly slower than you'd expect. And, as they all start competing for disk bandwidth, it will get much worse the longer it runs.

I'm also not sure about the use of the php memory_get_peak_usage() function; it is an 'internal' view of the memory the program requires, not the view from the operating system's perspective. It might be significantly worse. (Perhaps the interpreter requires 20 megs RSS just to run a hello-world. Perhaps not.)

I'm not sure what would be the best way forward for your application: maybe it could be a single long-lived process, that handles events as they are posted, and returns results. That might be able to run in significantly less memory space. Maybe the results don't actually change every 0.0005 seconds, and you could cache the results for one second, and only run it 86400 times per day. Or maybe you need to buy a few more machines. :)

sarnold
well said and thanks for the information. I will be hosting the application on cloud infrastructure so scalability will not be an issue but it doesn't hurt to know these variables ahead of time :P