Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

MySQL library for MQL4 with proper memory management - Library for MetaTrader 4

author EAcpu | 4 reads | 0 comments |

The original library file mql4-mysql.mqh is located here: https://www.mql5.com/en/code/11114 . Respect the work done by the original authors (originally by Russell , then modified by Wade Royd and Lukins /Sergey) and read the description of the original code.

The original mql4-mysql.mqh library had serious problems with memory management. Several users reported the problem (you can read it here: https://www.mql5.com/en/forum/28941 ).

Since I want to use the library as part of my project, I simply cannot afford the increase in memory, which would cause the system to crash. After inspecting the code, I managed to trace the problem back to the FetchArray function, which allows the user to read and save data from the database. The memory was not freed or allocated correctly, so I made some changes to the code to prevent memory leaks.

The improved library was tested and the memory consumption remained stable. The test lasted several hours and included thousands of connections/data reads, with a minimal increase in memory usage (less than 10 MB), which may be a result of MetaTrader 4 itself and the operation of the indicator (not a fresh installation of the MetaTrader 4 terminal). Based on my previous tests, the original library couldn't handle a few reads in the database and only worked for a few minutes before crashing. Testing is done using an EA, which requires data for each price tick.

IMPORTANT: For example, read the original description of the library!

 Integer database connection ID = 0 ;

It should be declared as a local variable, not a global variable. If you use the library with EAs or indicators, local declaration is required. This is done so that on each iteration, the variable memory address is different (automatically allocated by MetaTrader 4) and released after the loop.

You should also open connections for each specific task. It is not recommended to keep a connection open for multiple different requests. Example: I want to read a few rows from the database and save them into an array, I declare this procedure in a function. When the function is called, the connection is established, when the data is read, the connection is closed, and then the function returns XX data. The connection is open for the shortest amount of time.

Always remember to close the connection to deinit_MySQL, if you don't do this, connections will be established and over time you will reach the maximum number of connections allowed by the server running the database. The server will crash.

Proper transaction management should also be implemented so that two or more connections do not interfere with each other. Reading data violations may result, which will prevent your EA from working.

Apache server with MySQL DB for those who need it (free software):

If you want simple database, server and database management tools (it's free and user-friendly): https://www.apachefriends.org/index.html

Read the following to learn how to set up database protection: http://robsnotebook.com/xampp-builtin-security

Test it and please give feedback. This is the only way to solve the problem.

I could write a forum post with detailed examples of how to use this library and what is required to use it.

This is a fairly substantive subject, so I'll only write about it when necessary. If you would like me to write a user manual, please post a request.


Attachment download

📎 mql4-mysql.mqh (10.56 KB)

Source: MQL5 #13712

Verification code Refresh