如何减少VPS内存使用量

i have some problem with my VPS.

We are using a VPS to run our CMS and our websites, for now we have 300MB memory limit, and now we are close to reach the limit.

To maintain low cost(i know, increase memory is not to much expensive), but if i find a solution to optimize what we have, will be better.

What can i do?

Thanks!

I would suggest to increase memory - more memory, faster website :-) But if speed is not important, reduce all cache sizes, set php memory_limit to 8M, disable opcode caching (APC, eAccelerator) or try Raspberry Pi as server, now comes with 512MB :-)

What type of VPS is it? OpenVZ? Xen? KVM? If it is OpenVZ does it have VSwap or Burst memory?

What type of CMS / Website are you running? Is it PHP based? Are you using Apache? If so have you tried nginx? I would look at optimizing the web server component and removing unused processes/applications to reduce memory and increase performance.

I would look at Nginx like concerto49 recommended, if you have just one website on it also consider Litespeed (www.litespeedtech.com) they have a free version which may be sufficient to power your site.

If its PHP based, then strip out everything you're not using. Use APC/XCache to processing every request. Nginx also has a caching module, that could help so you can avoid hitting PHP for every request if its still fresh.

I have a small 256MB vps which uses apache + php + mariadb (mysqld)

I found memory was being gobbled by apache at every request 20MB at time for a simple wordpress page. It would settle but over time gobble into swap space and slow everything to a crawl. I suspect there are ways to fine tune mpm_event and mpm_worker to stop entering swap but I didn't figure out how.

When working in such tight environments it is important to know what is using what memory and reduce everything, so that swapping is minimized.

A summary of what I did follows, I managed to get 100MB physical headroom for tweaking, this is not a heavily loaded server but needs to be accessible (and cheap):

  • Using slackware (the os and default services seem to use less memory than debian, perhaps there is less loaded by default on my providers image)
  • Switch mysqld innodb tables off and use myisam
  • configure mysqld to reduce cache sizes if relevant
  • install cms fresh so they are definitely using myisam or alter all the tables

  • in apache choose to use the mod_mpm_prefork rather than mpm_event and mpm_worker ("apachectl -V" will tell you which is being used)

  • set sensible values for maxserver(start from low values for number of servers and requests and work up)

  • test the server under load whilst doing 'watch free' or 'top' through ssh

  • you should be able to see memory & the processes being created and destroyed as the load increases
  • adjust your httpd server settings and retest until happy

  • I like to see the memory max out at no more than 90% and reduce back down when the load is gone before I am convinced it will stay up without grinding to a halt (start using swap).

  • check your php.ini memory settings as stated in another answer.

  • I have also set a cron job to send me an email when swap starts to get used significantly, so I can restart something or even restart the whole server if I can't find what has used the memory, this should happen less and less the more you fine tune.

As stated this is not an environment that will perform well under heavy load but the cost may be more important to you.

Just my two pence worth...