LittleFunny
LittleFunny

Reputation: 8385

C#: Does passing complicated list of objects to another Class take a lot memory

This question not directly to mobile application. I needed to create a Command Pattern related structure for Undo and Redo functionality. The Command I will have is AddStrokeCommand, RemoveStrokeCommand and ClearAllCommand. Each of them can be undo.

I will need an Interface say ICommand with Execute and UnExecute.

Example of my code :

        public class AddStrokeCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        public ObservableCollection <Stroke> Strokes { get; set;}  

        public AddStrokeCommand (ObservableCollection<Stroke> strokes) {
            Strokes = strokes;
        }

        public bool CanExecute(object parameter)
        {
            throw new NotImplementedException();
        }

        public void Execute(object parameter)
        {
            Strokes.Add( (Stroke) parameter);
        }

        public void UnExecute()
        {
            Strokes.Remove( (Stroke) parameter); 
        }
    }

The application itself is showing many pages which can be annotated, so may have alot of Strokes drawn on each page. If I passed strokes to every command object, will it use a lot of memory? Will there be a better option?

Upvotes: 2

Views: 422

Answers (3)

Michel Keijzers
Michel Keijzers

Reputation: 15367

Normally, instances of objects are passed by reference, meaning they do not cause memory to be copied, only a pointer to the memory is passed.

This has the benefit that it does not cost memory usage, also not a high performance hit.

The disadvantage (but can be an advantage too), if you change the passed item, the changes will remain, even after returning to the caller function (because there is only a single instance of the data).

Here you can see more details: https://msdn.microsoft.com/en-us/library/4d43ts61%28v=vs.90%29.aspx

Upvotes: 3

InBetween
InBetween

Reputation: 32760

When you have the following code:

var myObject = new SomeVeryBigAndComplexObject();

What happens is that you create a new and very big object and store it in some memory previously allocated by the runtime. The value stored in myObject, if SomeVeryBigAndComplexObject is a reference type (a class), is not the object itself, its simply the memory address of where that object is.

So, when you do any of the following:

var obj = myObject;
Foo(myObject);

void Foo(SomeVeryAndComplexObject obj) { ... }

What you are really doing is simply copying the value stored in the reference myObj to obj, not the object itself. This is extremely cheap and you shouldn't worry about it at all.

Do bear in mind that all this is true for reference types. If the object were a value type (a struct) then the object itself would be copied because the object itself is the value of the variable myObject; that is one of the reasons why structs should always be small objects.

Upvotes: 1

Steve Cooper
Steve Cooper

Reputation: 21480

Passing a CLASS won't take any significant memory -- ususally just the few bytes used o hold the memory address. It essentially passes an integer which is the position of the object in virtual memory. (Google around for 'pass-by-reference semantics')

A STRUCT is a different matter. A struct is copied every time it's passed into a function or assigned to another variable, so you might find yourself making copies here. See 'pass-by-value semantics' for more.

Upvotes: 1

Related Questions