Analysis of Memory Usage in OpenVZ Virtualization is Not Like Dedicated Servers. Here is How To Sort Down Processes By Memory Usage on OpenVZ. It is actually a difficult work to explore the low-level resources that were allocated while from inside the container. The reason behind this kind of search is 6GB RAM server at $7/month price. Previously, we discussed in details about the memory matters inOpenVZ vs KVM article.
How To Sort Down Processes By Memory Usage on OpenVZ?
Normally on dedicated servers, we can check the GNU/Linux process memory usage with these commands :
ps aux --sort:rss
ps aux --sort -pid
If your OpenVZ instance is sucking 100% RAM when you can not explain the usage or data has mismatch (dashboard showing 100% usage but instance on SSH showing 40% usage), in such cases, you will see that the container is stable and processes are not generating Out of Memory (OOM) events. With 100% memory usage, it is not possible to expect a server to behave normally.
Itself the size reported by
ps has little relation to actual memory usage. It is the virtual size of each process which isn’t necessarily allocated memory. It also doesn’t include some segments that are allocated.
You can run these two commands :
egrep 'dcachesize|resource' /proc/user_beancounters
cat /proc/meminfo | egrep 'MemTotal|MemFree|Slab|SReclaimable'
smem is a tool that can give numerous reports on memory usage on Linux systems. Unlike the other tools,
smem can report proportional set size (PSS), which is a more meaningful representation of the amount of memory used by libraries and applications in a virtual memory system :
# not for server
PHP programs like this :
can show the near real usage. There is also
memrank tool which lists processes in the system sorted by decreasing PSS.
How To Decrease Memory Usage on OpenVZ?
Memory accounted in
Slab and in
SReclaimable is considered as cached but will be freed upon request. Webservers like nginx, apache2 can running inside of a container can create this situation. With normal OpenCZ host account you can not directly fix it, you need to run these commands on main machine :
# for setting to 256 Mb
vzctl set <CTID> --dcachesize 268435456:295279001 --save
# when memory management schema is SLM
vzctl set <CTID> --save --slmmode ubc
Documentations are here :
As normally you’ll not get that chance, you can use
/etc/security/limits.conf file and change the default size or better do a tweak with
ulimit. As example, processes launched from Apache can be altered from
/etc/init.d/apache2 script by 512Mb by adding :
ulimit -s 512
As lighttpd or nginx are multithreaded, so big stack per thread problem will remain. If you decrease the stack size any solution will have lower memory requirements.