Category Other  Published on 27/08/2015

Memory Manager Bot

Description

The memory manager when attached as robot periodically (configurable) frees the memory used by cAlgo thus keeping it down pressured.

Don't set the "Reclaim Period" to very low values which may effect the performance of cAlgo.

On each tick received it displays the current working set (memory used by cAlgo) and the last memory reclaimed.
Since it's checking to free or not on tick received event on slow trading hours the text will be updated less frequent.

The bot needs "FullAccess" permission to work, so you need to allow them when asked.

 


using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using cAlgo.API;

namespace MoneyBiz.TraderBots.Bots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MemoryManager : Robot
    {
        private long _lastFreeTime;
        private long _lastReclaimed;

        [Parameter("Reclaim Period (seconds)", DefaultValue = 60, MinValue = 10, Step = 10)]
        public int ReclaimPeriod { get; set; }

        protected override void OnStart()
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                Print("Memory Manager: Platform not supported.");
                Stop();

                return;
            }

            var stats = Free();

            DrawStats(stats);
        }

        protected override void OnTick()
        {
            var stats = Free();

            DrawStats(stats);
        }

        public void DrawStats(Tuple<long, long> stats)
        {
            if (stats != null)
            {
                _lastReclaimed = stats.Item1 - stats.Item2;
            }

            var currentWorkingSet = BytesToMegabytes(Environment.WorkingSet);
            var lastReclaimed = BytesToMegabytes(_lastReclaimed);
            var timeSpan = TimeSpan.FromTicks(DateTime.Now.Ticks - _lastFreeTime);

            var text = string.Format("Working Set: {1:N2} MB{0}Last Reclaimed: {2:N2} MB ({3:N0} s)", Environment.NewLine, currentWorkingSet, lastReclaimed, timeSpan.TotalSeconds);

            ChartObjects.DrawText("MemStats", text, StaticPosition.TopLeft, Colors.Orange);
        }

        private static double BytesToMegabytes(long value)
        {
            return (double)value / (1024 * 1024);
        }

        private Tuple<long, long> Free()
        {
            if (DateTime.Now.Ticks - _lastFreeTime <= TimeSpan.FromSeconds(ReclaimPeriod).Ticks)
            {
                return null;
            }

            Tuple<long, long> workingSets = null;

            try
            {
                using (var process = Process.GetCurrentProcess())
                {
                    var before = Environment.WorkingSet;

                    if (Environment.Is64BitProcess)
                    {
                        SetProcessWorkingSetSize64(process.Handle, -1, -1);
                    }
                    else
                    {
                        SetProcessWorkingSetSize32(process.Handle, -1, -1);
                    }

                    var after = Environment.WorkingSet;

                    workingSets = Tuple.Create(before, after);
                }

                _lastFreeTime = DateTime.Now.Ticks;

                Print("Memory Manager: Reclaimed {0:N0} bytes.", workingSets.Item1 - workingSets.Item2);
            } catch (Exception ex)
            {
                Print("Memory Manager: " + ex);
            }

            return workingSets;
        }

        [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        static internal extern bool SetProcessWorkingSetSize32(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

        [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        static internal extern bool SetProcessWorkingSetSize64(IntPtr pProcess, long dwMinimumWorkingSetSize, long dwMaximumWorkingSetSize);
    }
}


moneybiz's avatar
moneybiz

Joined on 06.12.2011

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Memory Manager.algo
  • Rating: 5
  • Installs: 3246
Comments
Log in to add a comment.
DE
dejese8013 · 6 months ago

THNKS FOR SHARE

TO
tomaifauchai · 1 year ago

This thing is the best BOT to have in your assets. (This page's free script) Everytime i run optimizations after 30 minutes 1 hour my memory is saturated and i upgraded to 64GB. cTrader has a very bad memory management for Optimizations.

I also tried the free trial just above...i'll tell you, download this free script and run it every 5-15 minutes if you're optimizing your strategies.

Spotware it's a shame you dont have this little code in your software because it seems it fix my memory issues and i can even backtests while running my 1-2 cbots during the week.

ClickAlgo's avatar
ClickAlgo · 4 years ago

Hi, FireMyst,

You are better off posting on the forum, posting on these threads do not alert anyone, I just found this by chance, the product below will free the memory used by the cTrader platform only, it was created to help users with a VPS and where they have backtested which allocates a large amount of memory.

https://clickalgo.com/ctrader-cbot-vps-memory-manager

moneybiz's avatar
moneybiz · 6 years ago

@ironmine: No, just once instance is enough.

IR
ironmine · 8 years ago

If I have, let's say, 10 charts opened, do I need to attach this Memory Manager cBot to each chart?

ChasBrownTH's avatar
ChasBrownTH · 8 years ago

Fantastic, this has saved me so much trouble! I was getting memory problems on a couple of 'cheap' VPS instances, as well as one damned expensive one, plus on local Hyper-V instances. This solved it all!

THANK YOU so much Moneybiz, this solution is very inspiring.

PS - I can understand why you stopped trying to Win with Forex, but for myself I am still hooked after 11 years ;)

 

EK
ekwam · 8 years ago

MoneBiz,

 

Thanks for your memory management algo .. i am using it on my VPS server.

 

Bravo..

tvman777's avatar
tvman777 · 8 years ago

Thank you for making this!! :D

aharonzbaida's avatar
aharonzbaida · 8 years ago

Thank you :)

ClickAlgo's avatar
ClickAlgo · 8 years ago

This is maybe best executing in hours/days and not seconds as each time it clears the memory, the platform has to get the data again, I am sure Spotware cache their data to reduce round-trip calls.

There could be a memory leak with the cAlgo platform or the sand-boxed cBots are not releasing the memory they use even with .NET's garbage collector, I don't know, but what i do know is every-time I stop and restart an instance to a cBot the memory increases.

This tool is useful for now and as I host my cBots on a VM with only 2 Gigs, i will set the memory manager  to 3600 seconds minimum or 1 hr or I will probably just clear the memory every 24hrs, which will save me restarting the cAlgo platform on the Virtual server at regular intervals.

anyway thank you very much :-)