vdemydiuk / mtapi

MetaTrader API (terminal bridge)
http://mtapi4.net/
MIT License
521 stars 281 forks source link

Mt4 memory leak #140

Closed vmuser99 closed 4 years ago

vmuser99 commented 5 years ago

I've setup a timer that calls every second the method CopyRates; this for retrieve the last 12 price of a closed candle. After about 30-35 cycle the loop halts; the Windows Task Manager shows that memory usage increases after every step in the loop. The same behavior happens in the TestApiUIClient; any advices? Maybe some memory leak in the api library?

KptKuck commented 5 years ago

Please give a working example to demonstrate this bug

vmuser99 commented 5 years ago

I've ported the example into MT5, but i have the same problem

`using System; using System.Threading.Tasks; using System.Threading; using MtApi5;

namespace FinTradeConsoleMt5 { class Program { static readonly object _locker = new object(); public static MtApi5Client client; static void Main(string[] args) { client = new MtApi5Client(); client.BeginConnect(8228);

        lock (_locker)
        {
            try
            {
                client.BeginConnect(8228);
                Thread.Sleep(500); // this for wait async connection...
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed connection!" + e.Message);

            }
            client.BeginConnect(8228);

            if (client.ConnectionState == Mt5ConnectionState.Connected)
            {
                Console.WriteLine("Connected!");
            }
        }

        TimerCallback callbackHourly = new TimerCallback(HourlyJob);
        //Timer hourlyTimer = new Timer(callbackHourly, null, TimeSpan.Zero, TimeSpan.FromHours(1.0));
        Timer hourlyTimer = new Timer(callbackHourly, null, 0, 100);           

        Console.WriteLine("Press ANY key to exit...");
        Console.ReadKey();
    }

    // Getting hourly Symbols data
    public static void HourlyJob(object state)
    {
        ExecuteCopyRates(client);
        Console.WriteLine("In TimerCallback: " + DateTime.UtcNow);

    }

    private static async void ExecuteCopyRates(MtApi5Client client)
    {
        var result = await Execute(() =>
        {
            MqlRates[] array;
            var count = client.CopyRates("EURUSD", ENUM_TIMEFRAMES.PERIOD_CURRENT, 1, 12, out array);
            return count > 0 ? array : null;
        });

        foreach (var rates in result)
        {
            //Console.WriteLine(
            //    $"time={rates.time}; mt_time={rates.mt_time}; open={rates.open}; high={rates.high}; low={rates.low}; close={rates.close}; tick_volume={rates.tick_volume}; spread={rates.spread}; real_volume={rates.tick_volume}");
            Console.WriteLine($"close={rates.close}");
        }

    }

    private static async Task<TResult> Execute<TResult>(Func<TResult> func)
    {
        return await Task.Factory.StartNew(() =>
        {
            var result = default(TResult);
            try
            {
                result = func();
            }
            catch (ExecutionException ex)
            {
                Console.WriteLine($"Exception: {ex.ErrorCode} - {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }

            return result;
        });
    }
}

} `

KptKuck commented 5 years ago

I have tested this for me. VS2017 last updates.

example stops after about 1 minute. But I do not recognize any memory leak.

Which task is affected?

I have tested the example in Matlab 2018b. Normal behavior.

The API does not seem to be affected.

If I compile the example as a debug version the problem is not there anymore.

Maybe a VS2017 problem

matlab output: CopyRates: result_count = 12 Executes: 1646 1.132170; 1.132210; 1.132220; 1.132090 1.132220; 1.132300; 1.132330; 1.132220 1.132330; 1.132290; 1.132330; 1.132250 1.132290; 1.132310; 1.132310; 1.132210 1.132300; 1.132280; 1.132360; 1.132280 1.132270; 1.132340; 1.132340; 1.132270 1.132330; 1.132250; 1.132330; 1.132250 1.132250; 1.132250; 1.132310; 1.132240 1.132250; 1.132400; 1.132470; 1.132250 1.132410; 1.132430; 1.132480; 1.132400 1.132420; 1.132400; 1.132440; 1.132380 1.132400; 1.132300; 1.132440; 1.132230 0 Connection changed: Disconnected memleak_test_matlab.zip

vdemydiuk commented 5 years ago

After migration to VS2017 I will test the projects for memory leaks.

vmuser99 commented 5 years ago

Ok, i'm using already VS2017