MySQL 5 cannot allocate memory

Hi, i’m running 32bit RedHat version 5 and after long uptime I always ran in problems with RedHat, I never had such problems with Debian which I normally use.

Long uptime:
00:10:01 up 347 days, 21:33, 1 user, load average: 0.77, 0.88, 0.90

I have more than enough free memory but after few days when mysql starts to use more memory it crashes.
total used free shared buffers cached
Mem: 4053 3822 231 0 187 2783
-/+ buffers/cache: 851 3202
Swap: 2047 41 2006

This is from error log, before that I had uptime on mysql around 120 days and I never had problems but after mysql restart and without changing any configuration settings Mysql is having hard time allocating memory.

Number of processes running now: 0
101203 20:40:19 mysqld restarted
101203 20:40:19 [Note] /usr/sbin/mysqld: ready for connections.
Version: ‘5.0.91-community-log’ socket: ‘/var/lib/mysql/mysql.sock’ port: 0 MySQL Community Edition (GPL)
101205 22:50:28 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101205 22:50:28 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101205 22:50:28 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101205 22:50:28 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101205 22:50:28 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101205 22:50:28 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101205 22:50:32 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101205 22:50:32 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101205 22:50:32 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101205 22:50:32 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101205 22:50:32 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101205 22:50:32 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:41 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:41 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:41 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:41 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:41 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:41 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:41 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:41 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:41 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:41 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:41 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:41 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:44 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:44 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:44 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:44 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:44 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:44 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:44 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:44 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:44 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:44 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:44 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:44 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:44 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:44 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:53 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:53 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:53 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:53 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:53 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:53 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:53 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:53 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:53 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:53 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:01:53 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:01:53 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:15:39 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:15:39 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:15:39 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:15:39 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 9:15:39 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101206 9:15:39 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 23:39:10 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 4054792 bytes)
101206 23:39:10 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101206 23:39:13 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 4054792 bytes)
101206 23:39:13 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101207 23:00:03 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 2096680 bytes)
101207 23:25:31 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1375668 bytes)
101207 23:25:31 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101207 23:25:31 - mysqld got signal 11 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
We will try our best to scrape up some info that will hopefully help diagnose
the problem, but since we have already crashed, something is definitely wrong
and this may fail.
key_buffer_size=268435456
read_buffer_size=2097152
max_used_connections=49
max_connections=200
threads_connected=4
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_connections = 1081344 K
bytes of memory
Hope that’s ok; if not, decrease some variables in the equation.

thd=0x9e5a4f80
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong…
Cannot determine thread, fp=0x9efa68c8, backtrace may not be correct.
Stack range sanity check OK, backtrace follows:
0x81827e5
0x81df06e
0x81df02d
0x81df02d
0x81df02d
0x81df02d
0x81df02d
0x81df02d
0x81e842e
0x81f01a6
0x81f0b5d
0x819aa08
0x81a091d
0x81a0e84
0x81a2e1f
0xb34832
0xa73f6e
New value of fp=(nil) failed sanity check, terminating stack trace!
Please read http://dev.mysql.com/doc/mysql/en/using-stack-trace.html and follow instructions on how to resolve the stack trace. Res$
stack trace is much more helpful in diagnosing the problem, so please do
resolve it
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort…
thd->query at 0x9c12f018 is invalid pointer
thd->thread_id=1313095
The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
information that should help you find out what is causing the crash.

Number of processes running now: 0
101207 23:25:31 mysqld restarted
101207 23:25:31 [Note] /usr/sbin/mysqld: ready for connections.
Version: ‘5.0.91-community-log’ socket: ‘/var/lib/mysql/mysql.sock’ port: 0 MySQL Community Edition (GPL)
101208 21:25:50 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1441176 bytes)
101208 21:25:50 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$
101208 21:25:50 [ERROR] /usr/sbin/mysqld: Out of memory (Needed 1441176 bytes)
101208 21:25:50 [ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use$

EDIT: Sorry wrong section, please move it to LAMP.

What is your:
ps aux |grep mysql

And why the heck are you using 32bit version ? This way you cannot use more that about 3GB for a process.

I got only 4GB of ram and my databases are small, so 3 years ago 32bit was OK.

mysql 10192 44.5 6.2 465412 257768 ? S<l Dec07 1568:23 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/server.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root 23331 0.0 0.0 5444 1120 ? S Nov23 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/v

What does ulimit -a report?

core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 73728
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) 524288
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 73728
virtual memory (kbytes, -v) 524288
file locks (-x) unlimited

can you interpret these results?

If max memory size is in KB, then max allocate size that process can allocate is 524288 or 524MB, and MySQL is using 6-8% which doesn’t even come close to that. And if MySQL doesn’t ran out of space why doesn’t it just use swap and not just crash.

key_buffer_size=268435456

How can it not even come close to 512MB? It cannot swap because swap memory is also limited.

gmouse wrote on Thu, 16 December 2010

Just to elaborate a bit more on what gmouse says.
The ulimit settings that limits your MySQL to 512M RAM is the hard limit for the process which has nothing to do with the swap.
Swap is an operating system feature that is used when the total RAM usage on the machine is about to reach the limit.

In your case you can choose to set the ulimit for memory to “unlimited” which means that you won’t box in all processes in the max 512M limit and instead there is only one hard limit which when the system runs out of memory (which is what you want I’m guessing).
I don’t run RHEL myself but according to some Googling you change this in the /etc/security/limits.conf file.
This page discusses changing the ulimit a lot:
http://www.puschitz.com/TuningLinuxForOracle.shtml

At the same time I have to ask a bit about the read_buffer_size variable setting that you have, do you really need it to be 2M when you allow 200 connections on a machine with so little RAM? That setting alone can occupy up to 400M of RAM. Are you really performing so many table scans? And if so could you possibly add indexes to avoid them?