473,569 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL consuming lots of memory during transactions w/ many statements

I am having a problem with MySQL consuming a lot of memory and
eventually throwing an Out of Memory error and restarting itself. The
symptoms are that swap usage continues to rise until some breaking
point. The application is a typical web application w/ 2 web servers
running Apache/Tomcat connecting to a dedicated DB server running only
MySQL.

This seems to occur as a result of running many statements in a single
transaction, both against InnoDB tables and MyISAM tables. In one
case, I'm writing all user actions to an audit table (MyISAM), all from
a single application thread doing approximately 5K inserts (as single
INSERTs on a single connection) every few seconds. In the other, I'm
doing a variety of select/update/insert/delete statements against
InnoDB tables, but again totaling several thousand in one transaction.
Both of these problems occur even when there is relatively low activity
elsewhere on the system.

Any help would be much appreciated. I will probably move the auditing
function onto a separate DB, but the other problem is harder to avoid
in my application. If breaking up the work into multiple transactions
would help, I can do that, but I'm hoping it's a configuration issue of
some kind instead, because this doesn't strike me as a volume that
should be an issue. But if this behavior is expected for some reason,
I can change my application appropriately.

My server:

MySQL 5.0.18
Enterprise Red Hat Linux 3.0 - kernel 2.4.21-37
Dual Xeon processors
4GB RAM
2GB Swap space

My my.cnf file:

# Max Connections (2 webapps w/ ~50 each, email w/ ~10, command-line)
# 200 should be plenty - max concurrent we've had is 90
max_connections =200

#
# InnoDB parameters
# innodb_buffer_p ool_size (512M)
# + key_buffer_size (50M)
# +
max_connections *(sort_buffer_s ize+read_buffer _size+binlog_ca che_size)
# 200 * (1M + 1M + 32K) = 406M
# + max_connections *2MB = 400M
# MUST NOT BE > 2GB
#
innodb_buffer_p ool_size = 512M # reduced from 1GB
innodb_addition al_mem_pool_siz e = 20M
# the default, set to 2 and can lose 1 sec tx but w/ better performance
innodb_flush_lo g_at_trx_commit = 1
innodb_log_buff er_size = 8M
# Set innodb_log_file _size between 1 and 1/(# logs) * buffer_pool_siz e
innodb_log_file _size = 512M
innodb_lock_wai t_timeout = 50 # default

#
# MyISAM params and others
#
skip-external-locking
key_buffer = 50M # Only few MyISAM tables, so this doesn't need to be
huge
max_allowed_pac ket = 16M
table_cache = 1024
sort_buffer_siz e = 1M
read_buffer_siz e = 1M
read_rnd_buffer _size = 4M
thread_cache_si ze = 8

# turn query cache off - all our queries are through prepared
statements
query_cache_typ e = 0
query_cache_siz e= 8M

thread_concurre ncy = 8
open_files_limi t=4096

Output from sar (I started the auditing process at 11:18 or so)

10:16:00 AM kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree
kbswpused %swpused kbswpcad
10:18:00 AM 53312 4042072 98.70 41872 2066840 1895504
144740 7.09 87180
10:20:00 AM 18036 4077348 99.56 41904 2052660 1895504
144740 7.09 87204
10:22:00 AM 69448 4025936 98.30 41736 2024472 1888508
151736 7.44 94248
10:24:00 AM 24636 4070748 99.40 41792 2059008 1888508
151736 7.44 94272
-------
11:18:00 AM 22892 4072492 99.44 42696 1978296 1889756
150488 7.38 91988
11:20:00 AM 114404 3980980 97.21 42268 1874360 1876972
163272 8.00 103576
11:22:00 AM 40180 4055204 99.02 42360 1936572 1871964
168280 8.25 108592
11:24:00 AM 20688 4074696 99.49 42544 1938640 1861324
178920 8.77 103696
11:26:00 AM 58724 4036660 98.57 42516 1910320 1854764
185480 9.09 95768
11:28:00 AM 25184 4070200 99.39 42464 1916800 1851216
189028 9.26 92692
11:30:00 AM 27652 4067732 99.32 42476 1918600 1846996
193248 9.47 96344
11:32:00 AM 25816 4069568 99.37 42852 1916080 1843812
196432 9.63 93896
11:34:00 AM 28332 4067052 99.31 42892 1912052 1843448
196796 9.65 88120
11:36:00 AM 23480 4071904 99.43 42832 1921680 1837276
202968 9.95 93836
11:38:00 AM 25360 4070024 99.38 42804 1905608 1834232
206012 10.10 88156
11:40:00 AM 20776 4074608 99.49 42920 1915008 1829128
211116 10.35 85244
11:42:00 AM 18048 4077336 99.56 42912 1924228 1825368
214876 10.53 85032
11:44:00 AM 53908 4041476 98.68 42772 1884004 1815200
225044 11.03 87628
11:46:00 AM 39784 4055600 99.03 42776 1908400 1809480
230764 11.31 84816
11:48:00 AM 21604 4073780 99.47 42864 1946080 1809480
230764 11.31 81416
11:50:00 AM 21092 4074292 99.48 42432 1940928 1809692
230552 11.30 77992
11:52:00 AM 17916 4077468 99.56 42348 1943732 1809692
230552 11.30 81092
11:54:00 AM 30684 4064700 99.25 42268 1933580 1809692
230552 11.30 81096
11:56:00 AM 18032 4077352 99.56 42204 1904984 1805388
234856 11.51 85504
11:58:00 AM 33072 4062312 99.19 42280 1888468 1803640
236604 11.60 79684
12:00:00 PM 28416 4066968 99.31 42232 1887656 1801864
238380 11.68 82088
12:02:00 PM 26012 4069372 99.36 42324 1886856 1801452
238792 11.70 80528
12:04:00 PM 24428 4070956 99.40 42368 1894824 1799092
241152 11.82 83652
12:06:00 PM 25156 4070228 99.39 42420 1927332 1798424
241820 11.85 83188
12:08:00 PM 18756 4076628 99.54 42452 1923044 1797544
242700 11.90 85988
12:10:00 PM 258124 3837260 93.70 42096 1719520 1775700
264544 12.97 94252
12:12:00 PM 146912 3948472 96.41 42252 1818536 1772992
267252 13.10 98516
12:14:00 PM 54248 4041136 98.68 42252 1901680 1770864
269380 13.20 103080
12:16:00 PM 21584 4073800 99.47 42052 1933524 1770864
269380 13.20 94728
12:18:00 PM 20040 4075344 99.51 41760 1728828 1766220
274024 13.43 114904
12:20:00 PM 54424 4040960 98.67 41144 1590524 1722632
317612 15.57 144496
12:22:00 PM 52568 4042816 98.72 41356 1690840 1715372
324872 15.92 160616

Jan 30 '06 #1
2 3803
"saran" <sa*********@gm ail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
I am having a problem with MySQL consuming a lot of memory and
eventually throwing an Out of Memory error and restarting itself.
Reading this page: http://dev.mysql.com/doc/refman/5.0/en/memory-use.html
the docs claim that there are no known memory leaks in MySQL, given that
they use several leak detection testing tools. This web page is interesting
reading, to understand how memory is used in MySQL.

If it is an error in the client, reading this page:
http://dev.mysql.com/doc/refman/5.0/...of-memory.html gives the
impression that the "Out of Memory" error is caused by a SQL query that
exceeds memory because of the size of the result set. This easily could be
caused by an accidental Cartesian product against relatively large tables.
You might want to review your queries throughout your code for possible
Cartesian products.
This seems to occur as a result of running many statements in a single
transaction, both against InnoDB tables and MyISAM tables.
Well, MyISAM tables do not support transactions. They are effectively
running under an "autocommit " mode. So if you see the error while running
against MyISAM tables, then it's not likely to be an issue of too many
statements per transaction.

Here's a thread on the MySQL Java forum that suggests that using updateable
result sets requires inserted rows to be cached on the client, and after a
few hundred thousand of these, causes an Out of Memory error:
http://lists.mysql.com/java/6297
Output from sar (I started the auditing process at 11:18 or so)

10:16:00 AM kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree
kbswpused %swpused kbswpcad
10:18:00 AM 53312 4042072 98.70 41872 2066840 1895504

.. . .

So your physical memory is pretty much pegged at 97-100% usage. I've seen
Linux abort MySQL and/or Tomcat processes when physical RAM is filled up
like this, even if there still seems to be some swap space available. If
you can't reduce the memory usage by changing your SQL queries, your options
may be either:
1) Add more RAM to your primary server, or else:
2) Run Tomcat and MySQL on separate hosts, and allocate appropriate physical
RAM on each host.

Regards,
Bill K.
Jan 30 '06 #2
Thanks for responding, Bill. Some clarification:
You might want to review your queries throughout your code for possible
Cartesian products.
For the situation in which the queries seem to be causing the problem,
the query in question will only ever return 100-500 rows. We do
however, run over a thousand of these queries in the same transaction.
And to get to those 100 rows, we're actually joining a very large table
on itself (> 6M rows), of couse with the proper where clauses in place
to restrict the results.
Well, MyISAM tables do not support transactions. They are effectively
running under an "autocommit " mode. So if you see the error while running
against MyISAM tables, then it's not likely to be an issue of too many
statements per transaction.
Right, what I meant for this situation since it's MyISAM and not InnoDB
is that all of the inserts are happening on the same connection,
without the connection ever being closed and re-opened. Is there any
state that would be held and not released on the server side that could
cause memory usage to grow? The table definition and insert we're
doing are:

CREATE TABLE db_audit (
query_id VARCHAR(100) NOT NULL,
query_sql TEXT NULL,
start_time DATETIME,
elapsed INTEGER,
INDEX (query_id),
INDEX (query_sql(50)) ,
INDEX (start_time)
) ENGINE = MYISAM;

INSERT INTO db_audit (query_id, query_sql, start_time, elapsed) VALUES
(a, b, c, d);

Any reason why running 5K of these simple inserts every few seconds
should use a lot of memory?
2) Run Tomcat and MySQL on separate hosts, and allocate appropriate physical
RAM on each host.


We are running Tomcat and MySQL on separate physical machines. The
MySQL box is not running anything but MySQL.

Thanks,
Saran

Jan 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1332
by: Paul | last post by:
I've just noticed this pecularity with mysql 4.1.0 running on windows, wondered if anyone could explain it / or is it a bug. I have a table called tokContact, which has a column called id, which has unique values from 1 up into the thousands with no gaps. This sql statement : select id from tokContact where id < 200 and id <> ALL...
0
328
by: Gacu | last post by:
Hi, I dont know what wrong. I have large main form class. At some point i create new Open File dialog, but when calling ShowDialog - out of memory exception occurs. I don't know why. I figured out that if I call this.hide() - hide main form - and then call ShowDialog() method of OpenFile - everything works fine.
1
4889
by: Aravind | last post by:
Hi, I am trying to create a image object out of a bitmap file like Image img = new Bitmap(@"c-\CurDisplayImage.bmp"); ,I found the memory of the process is increased twice as the bitmap size , for example if I load a 50MB bitmap the memory is increased by 100MB , Also when I draw the image with e.Graphics.DrawImage ,the Peak memory is...
5
1559
by: Ron Mexico | last post by:
I have written a graphing engine (very similar to what BigCharts.com offers). Basically, it's an ASPX page that accepts parameters and calls back-end business objects (dlls) to create a graph. When the graph is created as a jpeg, it is returned to the browser as binary data. When it is used at a low volume, it performs great and returns...
7
2959
by: W. Jordan | last post by:
Hello, I am using Response.WriteFile (filename) to write a file that on the server to my web client. I discover that the while a client request a big file, for example, a 14m-bytes one, the memory usage of the w3wp.exe increases the same amount of 14m. That might be horrible if I allow the users to download a 32m
0
1244
by: Patrox | last post by:
hi ! in mysql doc it is stated that : "MySQL supports local transactions (within a given client connection) " ref : http://dev.mysql.com/doc/refman/5.1/en/transactional-commands.html but what happens if for instance 2 different users tries to modify same data in 2 different transactions in 2 different connections ? does the last one which...
2
8250
by: speralta | last post by:
My tired old eyes may be failing me, but the following insert statements look correct to me, but I can't seem to get a clean insert from a fairly large text file database into mysql. I was wondering if maybe I'm hitting a limit on character length per line on some insert statements that I'm trying. Here's the error messages: execute...
3
5139
by: dan | last post by:
Hi all, I have a question about flash-memory lifespan. My database consists of 1 table, the table has 50 fields of 10 bytes each, thus each record will have 500bytes. There are 1000 records in the table, so roughly the table will have a size of 0.5MByte. The system will update the record sequentially, one record is updated per second....
9
9321
by: paulyche | last post by:
I'm writing a program which contains an awful lot of nested if statements. I don't know how efficient this is but it definetely makes the code less readable. Does anyone have any advice on how I can improve the following kind of code? Apparently other languages have a switch statement which is useful here...Python seems to lack this...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.