Math for Memory - knowing how much memory you need and how to optimize

From ZoneMinder Wiki
Jump to: navigation, search

<Home

It's very easy to underestimate how much memory you need to capture HD streams from cameras. When I first started, I assumed 2GB memory was sufficient to capture 5 HD cameras, as long as I reduce frame rate a bit. I never really did the math, but very soon I noticed that ZM was reporting all sorts of errors, cameras kept failing and all sorts of errors.

So I asked for help here and user knnniggett offered excellent sage advice.

So do yourself a favor and do this math: (and please read this for a better understanding)

Min Memory = 1.2 * ((image-width*image-height*image buffer size*target color space*number of cameras/8/1024/1024 ) 

Where:

  • image-width and image-height are the width and height of images that your camera is configured for (in my case, 1280x960). This value is in the Source tab for each monitor
  • image buffer size is the # of images ZM will keep in memory (this is used by ZM to make sure it has pre and post images before detecting an alarm - very useful because by the time an alarm is detected, the reason for the alarm may move out of view and a buffer is really useful for this, including for analyzing stats/scores). This value is in the buffers tab for each monitor
  • target color space is the color depth - 8bit, 24bit or 32bit. It's again in the source tab of each monitor
  • The 1.2 at the start is basically adding 20% on top of the calculation to account for image/stream overheads (this is an estimate)

So let's do the math. I have 4 cameras running at 1280x960 with 32bit color space. I have one camera running at 640x480 with 8bit greyscale color space

So, my system would require:

1.2 * ((1280*960*50*32*4/8/1024/1024 )  + (640 *480  *50*8/8 /1024/1024))

Or, around 900MB of memory.

And I have twice that memory. Sufficient!

No, not really:

  • This is just the base memory required to capture the streams. Remember ZM is always capturing streams irrespective of whether you are actually recording or not - to make sure its image ring buffer is there with pre images when an alarm kicks in.
  • You also need to account for other processes not related to ZM running in your box
  • You also need to account for other ZM processes - for example, I noticed the audit daemon takes up a good amount of memory when it runs, DB updates also take up memory


Okay, fine, but I have double the memory (2GB) and ZM only needs, say, 1.5GB even if take your points above in. And its a dedicated machine for ZM

Well, as it turns out, ZM uses mapped memory and by default, 50% of your physical memory is what this will grow to. In other words, in my case 1GB. When you reach that limit, ZM breaks down with various errors.

Now, you can obviously allocate more space for this. Infact, in my case, I increased it to 70% of physical memory. But before you do, remember it may have an effect on other applications not related to ZM. If you want to increase it to 70%, add the following to /etc/fstab

tmpfs /run/shm tmpfs defaults,noexec,nosuid,size=70% 0 0

You can always validate how much mapped memory is allocated to ZM by doing this:

ls -l /run/shm/zm* | awk 'BEGIN {sum=0} {sum += $5} END {print sum/1024/1024."M"}'

I eventually upgraded my system to 4GB and increased mapped memory to 70% and I've not had any issues or bad spikes.