ReactiveUI icon indicating copy to clipboard operation
ReactiveUI copied to clipboard

High memory usage with ObservableAsPropertyHelpers?

Open dchaib opened this issue 11 years ago • 3 comments

I have recently refactored an MVVM application to use ReactiveUI. Basically, I have changed a lot of the code in the ViewModels to use ReactiveLists, derived lists and ObservableAsPropertyHelpers. Once I was done, I noticed that the memory usage of this application went from 100MB to 550MB (in the exact same situation, with the same data and features).

I have a large number (~3000) of objects from the same class and using the diagnostics tools from VS, I noticed that the OAPH from this class (they are 5) used a lot of memory. At first, I assumed an issue with threading so I decided to try and reproduce the issue in a test application.

To do so I created a ViewModel:

public class ItemViewModel : ReactiveObject
{
    private ObservableAsPropertyHelper<bool> myVar1;
    public bool MyProperty1
    {
        get { return myVar1.Value; }
    }

    public ItemViewModel()
    {
        myVar1 = Observable.Return(false).ToProperty(this, x => x.MyProperty1);
    }
 }

(I have removed the 3 other OAPH that are stricly identical for brievity.)

My main ViewModel looks like this:

public class MainViewModel : ReactiveObject
{
    public IReactiveList<ItemViewModel> Items { get; private set; }

    public MainViewModel()
    {
        var items = new List<ItemViewModel>();
        for (int i = 0; i < 3000; i++)
        {
            items.Add(new ItemViewModel());
        }
        Items = new ReactiveList<ItemViewModel>(items);
    }
}

That's all there is in this application. If I run it, it uses 35MB. If I remove the code in the constructor of ItemViewModel, it only uses 11.5MB. So it seems to reproduce my high memory usage.

My question is: am I doing something wrong? Can ReactiveUI handle that "many" items?

dchaib avatar Mar 15 '15 09:03 dchaib

I have exactly the same problem.

bamboocharacing avatar Apr 13 '15 07:04 bamboocharacing

yep same issue here. say we create 100 000 objects displayed in a grid. the non RxUI version will consume 25MB, the RxUI version more than 1GB.

    public class RowViewModelNoRxUI : INotifyPropertyChanged
    {
        private int _prop1;
        private int _prop2;
        private int _prop3;
        private int _prop4;
        private int _prop5;
        private int _prop6;

        public int Prop1 => _prop1;
        public int Prop2 => _prop2;
        public int Prop3 => _prop3;
        public int Prop4 => _prop4;
        public int Prop5 => _prop5;
        public int Prop6 => _prop6;

        private Model _model;

        public Model Model
        {
            get { return _model; }
            set
            {
                _model = value;
                _prop1 = _model.Id + 1;
                _prop2 = _model.Id + 2;
                _prop3 = _model.Id + 3;
                _prop4 = _model.Id + 4;
                _prop5 = _model.Id + 5;
                _prop6 = _model.Id + 6;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class RowViewModelRxUI : ReactiveObject, IRowViewModel
    {
        private ObservableAsPropertyHelper<int> _prop1;
        private ObservableAsPropertyHelper<int> _prop2;
        private ObservableAsPropertyHelper<int> _prop3;
        private ObservableAsPropertyHelper<int> _prop4;
        private ObservableAsPropertyHelper<int> _prop5;
        private ObservableAsPropertyHelper<int> _prop6;

        public int Prop1 => _prop1.Value;
        public int Prop2 => _prop2.Value;
        public int Prop3 => _prop3.Value;
        public int Prop4 => _prop4.Value;
        public int Prop5 => _prop5.Value;
        public int Prop6 => _prop6.Value;

        private Model _model;

        public Model Model
        {
            get { return _model; }
            set { this.RaiseAndSetIfChanged(ref _model,value); }
        }
        
        public RowViewModelRxUI()
        {
            _prop1 = this.WhenAnyValue(t => t.Model).Where(v => v != null).Select(m => m.Id + 1).ToProperty(this, v => v.Prop1);
            _prop2 = this.WhenAnyValue(t => t.Model).Where(v => v != null).Select(m => m.Id + 2).ToProperty(this, v => v.Prop2);
            _prop3 = this.WhenAnyValue(t => t.Model).Where(v => v != null).Select(m => m.Id + 3).ToProperty(this, v => v.Prop3);
            _prop4 = this.WhenAnyValue(t => t.Model).Where(v => v != null).Select(m => m.Id + 4).ToProperty(this, v => v.Prop4);
            _prop5 = this.WhenAnyValue(t => t.Model).Where(v => v != null).Select(m => m.Id + 5).ToProperty(this, v => v.Prop5);
            _prop6 = this.WhenAnyValue(t => t.Model).Where(v => v != null).Select(m => m.Id + 6).ToProperty(this, v => v.Prop6);
        }
    }
}

rhautefeuille avatar Nov 10 '17 10:11 rhautefeuille

Assigning to myself, see if it still is an issue with latest.

glennawatson avatar Sep 01 '18 00:09 glennawatson