Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Releasing memory of Custom Series elements

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Releasing memory of Custom Series elements

    In my indicator, I use a custom series with my own class.

    On each new bar, I create a new class, which also creates and manage SortedList, and assign it to the series.

    To keep my memory managed well, do I need to clear or release memory occupied by these classes on OnStateChange State=Terminated, or when the chart refresh?

    To clear, here is an example of my series and my class:

    Code:
    		private Series<TPObar> 			TPOseries;
    Code:
    			TPOseries = new Series<TPObar>(this);
    Code:
    	public class TPObar {
    		public SortedList<double, TPObarPrice> TPObarPrices;
    		
    		public TPObar()
    		{
    			TPObarPrices = new SortedList<double, TPObarPrice>();			
    		}
    		
    		public void Clear()
    		{
    			if (TPObarPrices != null)
    			{
    				TPObarPrices.Clear();
    			}
    		}
    	}
    and here is how I create and assign it on each bar:

    Code:
    protected override void OnBarUpdate()
    		{
    				...
    				...
    				TPObar CurrTPObar;
    				CurrTPObar = TPOseries[0];
    				if (CurrTPObar == null)
    				{
    					// New bar, create the TPObar
    					CurrTPObar = new TPObar();
    				}
    					
    				...
    				my logic
    				...
    				TPOseries[0] = CurrTPObar;
    				...
    		}

    #2
    Thank you for your question Shai Samuel.


    Since SortedList doesn't implement IDisposable, .NET's garbage collection will need to be relied upon to handle the internal memory for each TPOBar. Similarly, you also do not have a point of control over your Series, as NinjaTrader's engine handles memory clean up for Series (which is why there is no special OnTermination handling in the documentation (linked) )


    Therefore, OnTerminated isn't the right place if you would like to manage memory; at this point .Net's garbage collector will do all the work for you.



    However, if you would like to prevent your script from amassing a large amount of memory during runtime, you can null out references using the Series' Reset method (documentation linked) during OnBarUpdate everywhere you are positive you won't need a particular bar object. You can then check to see if you've previously reset a bar using IsValidDataPointAt (documentation linked). This could be done, for example, once you've reached State.Realtime on all of your historical bars.
    Jessica P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by samish18, 04-17-2024, 08:57 AM
    16 responses
    55 views
    0 likes
    Last Post samish18  
    Started by arvidvanstaey, Today, 02:19 PM
    3 responses
    9 views
    0 likes
    Last Post NinjaTrader_Zachary  
    Started by jordanq2, Today, 03:10 PM
    2 responses
    8 views
    0 likes
    Last Post jordanq2  
    Started by traderqz, Today, 12:06 AM
    10 responses
    18 views
    0 likes
    Last Post traderqz  
    Started by algospoke, 04-17-2024, 06:40 PM
    5 responses
    47 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X