In certain (randomly, i think) moments every about 1-2 days, my server hangs. The free memory going down to zero and i have maaaany php-cgi processes from the same website. The website has not a great traffic; about 300 unique visits per day.

When the server hangs, i must restart apache (the server in on heavy load, so it needs 2-3 minutes for completing the operation)
Then, EVERY TIME the server hangs, i must restart amavis and clamav, that dies without any reason:

Looks like a problem with disk I/O to me. You should try to optimize MySQL server settings and set up caching for your web sites (maybe in combination with memcached or with a memory file system like tmpfs).

This is the output of tuning-primer after i modified some mysqld settings:

Code:

[email protected]:~# ./tuning-primer.sh
-- MYSQL PERFORMANCE TUNING PRIMER --
- By: Matthew Montgomery -
MySQL Version 5.1.58-1ubuntu1-log x86_64
Uptime = 0 days 14 hrs 31 min 13 sec
Avg. qps = 15
Total Questions = 802751
Threads Connected = 1
Warning: Server has not been running for at least 48hrs.
It may not be safe to use these recommendations
To find out more information on how each of these
runtime variables effects performance visit:
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html
Visit http://www.mysql.com/products/enterprise/advisors.html
for info about MySQL's Enterprise Monitoring and Advisory Service
SLOW QUERIES
The slow query log is enabled.
Current long_query_time = 10.000000 sec.
You have 4947 out of 802772 that take longer than 10.000000 sec. to complete
Your long_query_time seems to be fine
BINARY UPDATE LOG
The binary update log is NOT enabled.
You will not be able to do point in time recovery
See http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html
WORKER THREADS
Current thread_cache_size = 8
Current threads_cached = 7
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine
MAX CONNECTIONS
Current max_connections = 100
Current threads_connected = 1
Historic max_used_connections = 33
The number of used connections is 33% of the configured maximum.
Your max_connections variable seems to be fine.
INNODB STATUS
Current InnoDB index space = 192 K
Current InnoDB data space = 128 K
Current InnoDB buffer pool free = 91 %
Current innodb_buffer_pool_size = 8 M
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory
MEMORY USAGE
Max Memory Ever Allocated : 304 M
Configured Max Per-thread Buffers : 456 M
Configured Max Global Buffers : 154 M
Configured Max Memory Limit : 610 M
Physical Memory : 1.96 G
Max memory limit seem to be within acceptable norms
KEY BUFFER
Current MyISAM index space = 12 M
Current key_buffer_size = 64 M
Key cache miss rate is 1 : 6489
Key buffer free ratio = 77 %
Your key_buffer_size seems to be fine
QUERY CACHE
Query cache is enabled
Current query_cache_size = 80 M
Current query_cache_used = 76 M
Current query_cache_limit = 4 M
Current Query cache Memory fill ratio = 95.87 %
Current query_cache_min_res_unit = 4 K
However, 68135 queries have been removed from the query cache due to lack of memory
Perhaps you should raise query_cache_size
MySQL won't cache query results that are larger than query_cache_limit in size
SORT OPERATIONS
Current sort_buffer_size = 2 M
Current read_rnd_buffer_size = 256 K
Sort buffer seems to be fine
JOINS
Current join_buffer_size = 2.00 M
You have had 2 queries where a join could not use an index properly
You should enable "log-queries-not-using-indexes"
Then look for non indexed joins in the slow query log.
If you are unable to optimize your queries you may want to increase your
join_buffer_size to accommodate larger joins in one pass.
Note! This script will still suggest raising the join_buffer_size when
ANY joins not using indexes are found.
OPEN FILES LIMIT
Current open_files_limit = 1110 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine
TABLE CACHE
Current table_open_cache = 500 tables
Current table_definition_cache = 16384 tables
You have a total of 202 tables
You have 227 open tables.
The table_cache value seems to be fine
TEMP TABLES
Current max_heap_table_size = 256 M
Current tmp_table_size = 256 M
Of 15040 temp tables, 32% were created on disk
Perhaps you should increase your tmp_table_size and/or max_heap_table_size
to reduce the number of disk-based temporary tables
Note! BLOB and TEXT columns are not allow in memory tables.
If you are using these columns raising these values might not impact your
ratio of on disk temp tables.
TABLE SCANS
Current read_buffer_size = 128 K
Current table scan ratio = 154 : 1
read_buffer_size seems to be fine
TABLE LOCKING
Current Lock Wait ratio = 1 : 6331
Your table locking seems to be fine

It tells me i must expand the max_heap_table_size to a value > 256M. I try 352MB?