"Memory management" in cbots

Created at 21 Mar 2024, 08:51
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CT

ctid4921325

Joined 18.11.2023

"Memory management" in cbots
21 Mar 2024, 08:51


Hi there

I often experience ctrader grinding to a halt over the course of the trading day, and as it does, its memory foot print grows signficantly, once over 2gb it starts to slow down, by by 5gb (in a 32gb machine) its unresponsive. Some times it bloats beyond 20gb.

I'm going to assume some cbot or indicator i've made is at fault, and i see in the task manager by expanding the ctrader entry, there can be large “algohosts”.

So, i think my question is, whats the key to not having memory leaks/bloats in cbots/c# ? what do i need to be looking for ? I kind of assumed some global variable holding some object that was constantly added to would be the cause, but im not so sure.

I'm not a c# expert, everything I know about c# comes from using ctrader. Anyhow grateful for any insights. 

Glenn


@ctid4921325
Replies

firemyst
22 Mar 2024, 01:15 ( Updated at: 22 Mar 2024, 07:52 )

Most of the newbies I've helped, their memory issues stem from having a lot of Print statements in their code. Printing lots of information to the logs will suck up memory since there's no automated way to clear it.

The other big problem is if your indicators/bots draw A LOT of chart objects. cTrader cannot easily handle hundreds and hundreds of custom chart objects being drawn on charts. For instance, if you draw your own custom TrendLine objects, once you hit about 1,000 objects, performance will degrade noticeably. So you have to have your own built in methods to clean up objects, or limit the number you draw on any chart continuously. Example: if you use TrendLines to draw your own Heikin Ashi candles on a chart overlaying normal candles on say an M1 chart, you'll easily end up with over 500 HA bars on top of the regular M! bars. So what you need to do is limit it so your indicator/bot only draws the most recent 500 HA bars, removing the previous ones on each new bar drawn. 

And for such drawings, you only want it to happen “OnBar” and not “OnTick” where possible – redrawing everything on every tick is very resource intensive, especially if you have multiple indicators/bots doing it at the same time.


@firemyst

ctid4921325
22 Mar 2024, 07:58

RE: "Memory management" in cbots

firemyst said: 

Most of the newbies I've helped, their memory issues stem from having a lot of Print statements in their code. Printing lots of information to the logs will suck up memory since there's no automated way to clear it.

The other big problem is if your indicators/bots draw A LOT of chart objects. cTrader cannot easily handle hundreds and hundreds of custom chart objects being drawn on charts. For instance, if you draw your own custom TrendLine objects, once you hit about 1,000 objects, performance will degrade noticeably. So you have to have your own built in methods to clean up objects, or limit the number you draw on any chart continuously. Example: if you use TrendLines to draw your own Heikin Ashi candles on a chart overlaying normal candles on say an M1 chart, you'll easily end up with over 500 HA bars on top of the regular M! bars. So what you need to do is limit it so your indicator/bot only draws the most recent 500 HA bars, removing the previous ones on each new bar drawn. 

And for such drawings, you only want it to happen “OnBar” and not “OnTick” where possible – redrawing everything on every tick is very resource intensive, especially if you have multiple indicators/bots doing it at the same time.

Ahah.. fantastic thanks firemyst.. yes, I am outputing hundreds of lines of print.  I have the print wrapped in a method called Debug, which skips or pints based parameters, but it is on. I will turn those off and see what happens.

I do a little bit of drawing, but i'm not approaching 100 objects let alone 1000, and I've moved almost everything to OnBar out of ontick.

Thanks for the tip, I'll turnon/reduce my prints and we'll see what that does.

 

Thanks again.


@ctid4921325

firemyst
22 Mar 2024, 08:01

RE: RE: "Memory management" in cbots

ctid4921325 said: 

firemyst said: 

Most of the newbies I've helped, their memory issues stem from having a lot of Print statements in their code. Printing lots of information to the logs will suck up memory since there's no automated way to clear it.

The other big problem is if your indicators/bots draw A LOT of chart objects. cTrader cannot easily handle hundreds and hundreds of custom chart objects being drawn on charts. For instance, if you draw your own custom TrendLine objects, once you hit about 1,000 objects, performance will degrade noticeably. So you have to have your own built in methods to clean up objects, or limit the number you draw on any chart continuously. Example: if you use TrendLines to draw your own Heikin Ashi candles on a chart overlaying normal candles on say an M1 chart, you'll easily end up with over 500 HA bars on top of the regular M! bars. So what you need to do is limit it so your indicator/bot only draws the most recent 500 HA bars, removing the previous ones on each new bar drawn. 

And for such drawings, you only want it to happen “OnBar” and not “OnTick” where possible – redrawing everything on every tick is very resource intensive, especially if you have multiple indicators/bots doing it at the same time.

Ahah.. fantastic thanks firemyst.. yes, I am outputing hundreds of lines of print.  I have the print wrapped in a method called Debug, which skips or pints based parameters, but it is on. I will turn those off and see what happens.

I do a little bit of drawing, but i'm not approaching 100 objects let alone 1000, and I've moved almost everything to OnBar out of ontick.

Thanks for the tip, I'll turnon/reduce my prints and we'll see what that does.

 

Thanks again.

Keep us posted please.

In my bots/indicators, I have a “debug” flag as a parameter that I can set when I run them. Of course, if true it will print out lots of info to the log; when false it doesn't print anything from the code. I've found this to be a great approach.


@firemyst

ctid4921325
22 Mar 2024, 08:03

RE: RE: RE: "Memory management" in cbots

firemyst said: 

ctid4921325 said: 

firemyst said: 

Most of the newbies I've helped, their memory issues stem from having a lot of Print statements in their code. Printing lots of information to the logs will suck up memory since there's no automated way to clear it.

The other big problem is if your indicators/bots draw A LOT of chart objects. cTrader cannot easily handle hundreds and hundreds of custom chart objects being drawn on charts. For instance, if you draw your own custom TrendLine objects, once you hit about 1,000 objects, performance will degrade noticeably. So you have to have your own built in methods to clean up objects, or limit the number you draw on any chart continuously. Example: if you use TrendLines to draw your own Heikin Ashi candles on a chart overlaying normal candles on say an M1 chart, you'll easily end up with over 500 HA bars on top of the regular M! bars. So what you need to do is limit it so your indicator/bot only draws the most recent 500 HA bars, removing the previous ones on each new bar drawn. 

And for such drawings, you only want it to happen “OnBar” and not “OnTick” where possible – redrawing everything on every tick is very resource intensive, especially if you have multiple indicators/bots doing it at the same time.

Ahah.. fantastic thanks firemyst.. yes, I am outputing hundreds of lines of print.  I have the print wrapped in a method called Debug, which skips or pints based parameters, but it is on. I will turn those off and see what happens.

I do a little bit of drawing, but i'm not approaching 100 objects let alone 1000, and I've moved almost everything to OnBar out of ontick.

Thanks for the tip, I'll turnon/reduce my prints and we'll see what that does.

 

Thanks again.

Keep us posted please.

In my bots/indicators, I have a “debug” flag as a parameter that I can set when I run them. Of course, if true it will print out lots of info to the log; when false it doesn't print anything from the code. I've found this to be a great approach.

yeah thats exactly what i've done, I also allow it to override inside a function so that only debug from selected functions prints out, though that was for clutter in the log, not for performance reasons.   Testing now… will let you know what i learn.


@ctid4921325

ctid4921325
22 Mar 2024, 09:20

RE: RE: RE: RE: "Memory management" in cbots

ctid4921325 said: 

firemyst said: 

ctid4921325 said: 

firemyst said: 

Most of the newbies I've helped, their memory issues stem from having a lot of Print statements in their code. Printing lots of information to the logs will suck up memory since there's no automated way to clear it.

The other big problem is if your indicators/bots draw A LOT of chart objects. cTrader cannot easily handle hundreds and hundreds of custom chart objects being drawn on charts. For instance, if you draw your own custom TrendLine objects, once you hit about 1,000 objects, performance will degrade noticeably. So you have to have your own built in methods to clean up objects, or limit the number you draw on any chart continuously. Example: if you use TrendLines to draw your own Heikin Ashi candles on a chart overlaying normal candles on say an M1 chart, you'll easily end up with over 500 HA bars on top of the regular M! bars. So what you need to do is limit it so your indicator/bot only draws the most recent 500 HA bars, removing the previous ones on each new bar drawn. 

And for such drawings, you only want it to happen “OnBar” and not “OnTick” where possible – redrawing everything on every tick is very resource intensive, especially if you have multiple indicators/bots doing it at the same time.

Ahah.. fantastic thanks firemyst.. yes, I am outputing hundreds of lines of print.  I have the print wrapped in a method called Debug, which skips or pints based parameters, but it is on. I will turn those off and see what happens.

I do a little bit of drawing, but i'm not approaching 100 objects let alone 1000, and I've moved almost everything to OnBar out of ontick.

Thanks for the tip, I'll turnon/reduce my prints and we'll see what that does.

 

Thanks again.

Keep us posted please.

In my bots/indicators, I have a “debug” flag as a parameter that I can set when I run them. Of course, if true it will print out lots of info to the log; when false it doesn't print anything from the code. I've found this to be a great approach.

yeah thats exactly what i've done, I also allow it to override inside a function so that only debug from selected functions prints out, though that was for clutter in the log, not for performance reasons.   Testing now… will let you know what i learn.

Right on the money Firemyst… things are running much smoother, and there's been no noticable growth in memory foot print.  Thanks so much.


@ctid4921325