Bug #41440 Out of memory error "It is possible that mysqld could use" incorrect size
Submitted: 12 Dec 2008 16:00 Modified: 15 Dec 2008 7:47
Reporter: Shannon Wade Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Errors Severity:S3 (Non-critical)
Version: 4.1.22, 5.1.30, 4.1, 5.0, 5.1, 6.0 bzr OS:Any
Assigned to: CPU Architecture:Any

[12 Dec 2008 16:00] Shannon Wade
Description:
mysqld crashed due to out of memory, message in error log indicated 1635594k , 1.5 G, but really could have used up to 5.5G

---
key_buffer_size=100663296
read_buffer_size=2093056
max_used_connections=572
max_connections=700
threads_connected=462
It is possible that mysqld could use up to 
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_connections = 1635594 K
bytes of memory
Hope that's ok; if not, decrease some variables in the equation.
--

global variables show sort_buffer_size to be:

key_buffer_size 100663296

read_buffer_size 2093056
sort_buffer_size 6291448

sum of session buffers 8384504

max_connections 700

max_connections * sum of session buffers = 5869152800

plus key buffer 5969816096 bytes (5829898.53125 k)

Due to wrapping around the 32-bit mark
5969816096 - (2^32) = 1674848800 = 1635594.53125 K

Source is casting as ulong (from 5.1.30 src):

fprintf(stderr, "It is possible that mysqld could use up to \n\
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = %lu K\n\
bytes of memory\n", ((ulong) dflt_key_cache->key_cache_mem_size +
                     (global_system_variables.read_buff_size +
                      global_system_variables.sortbuff_size) *
                     thread_scheduler.max_threads +
                     max_connections * sizeof(THD)) / 1024);
  fprintf(stderr, "Hope that's ok; if not, decrease some variables in the equation.\n\n");

How to repeat:
exceed possible value for ulong datatype

Suggested fix:
Change ulong cast to ulonglong
[12 Dec 2008 23:41] MySQL Verification Team
this entire formula is bogus anyway, as I reported in bug #35661
[15 Dec 2008 7:47] Sveta Smirnova
Thank you for the report.

Verified as described.
[22 Feb 2011 10:07] MySQL Verification Team
this should just be closed as a duplicate of bug #35661

mysqld shouldn't crash due to out of memory. we expect some out of memory error message instead. if there is a testcase, then it should be filed separately using "low memory crash when X Y Z ..." synopsis.