Best way for memory to pass method parameters

I am experiencing some memory performance issues with my code and I am not sure which is the best way to pass parameters to my methods. I will give a short example about this.

I have for instance this class:

class Person
    public string Name { get; set; }
    public bool IsMale { get; set; }
    public string Address { get; set; }
    public DateTime DateOfBirth { get; set; }

and then I am using it like this:

    static void Main(string[] args)
        Person person = new Person {
            Name = "John",
            Address = "Test address",
            DateOfBirth = DateTime.Now,
            IsMale = false };

        int age = CalculateAge(person);

        int age2 = CalculateAge(person.DateOfBirth);

    private static int CalculateAge(Person person)
        return (DateTime.Now - person.DateOfBirth).Days;

    private static int CalculateAge(DateTime birthDate)
        return (DateTime.Now - birthDate).Days;

Now in my app I have lots of method calls where the entire object (an object with lots of properties not like this "Person" object from my example) was passed as parameter and I am thinking to improve the code by sending to the methods only the properties that they need, but currently I am not sure how this will improve memory performance.

How is it better regarding memory usage, to send the entire Peron object to my method like CalculateAge(person); or to send just the property that is used into the method like CalculateAge(person.DateOfBirth);?

First I thought that calls like this CalculateAge(person.DateOfBirth); (sending as parameters only the needed properties instead of the entire object) are using less memory but in the end after tests I noticed that app performs slower, and now I am not sure if these changes or others were slowing down my app.


There are three situations to consider here:

  1. Passing a class object vs. another class object
  2. Passing a struct value vs. a class object
  3. Passing multiple class/structs vs. a single class

In terms of memory only the first case is going to be an equal trade: it takes as much memory to pass a string Name as it takes to pass Person person.

Passing a struct, such as DateTime, may take more memory or less memory, depending on the size of the struct. For DateTime the size is (currently) eight bytes. For a smaller by-value object, e.g. a struct with a single short, you would need fewer bytes. For larger structs, e.g. DateTimeOffset, you would need more bytes.

Passing multiple objects or values in most cases is going to require more memory than passing a single class object. The only exception would be passing several very small structs.

Independently from considering the memory impact you should consider the logical structure of your API before deciding on the strategy.

Passing a Person to an API that computes the age creates a dependency between the age-computing API and the Person. Passing DOB separately, on the other hand, creates a dependency between Person and the caller of the API.

If you plan to use age-computing API exclusively with Person objects, then passing Person makes sense. Moreover, you should consider adding a computed property directly to the Person class,

On the other hand, if you plan to use age-computing API with other objects (animals, buildings, etc.) you would be better off passing DOB.

Basically, if I'm not completely wrong, this should not matter. All that is passed to the method is a pointer to your object, which is 8 byte on a 64-bit machine (see here).

It should only make a difference if you can reduce the number of objects you pass (n times 8 byte), but also this should never be a problem in a "standard application".

EDIT: As @Bashn pointet out, DateTime is a struct and is therefor completely copied to the stack on method call. Yet it does not matter in this case, because DateTime is 8 bytes big. Depending on the size of the given struct, this may impose more or less overhead on method calling.

Still, passing paramters should never lead to a memory problem. How did you find out your memory issue?

Need Your Help

Does continous streaming of positions work for a HTML5 action-intensive server-client game?

html5 networking websocket real-time bandwidth

In an action intensive (fighting-style) game designed for ~100 players, is it sufficient to stream the positions of every player to every player via websocket, considering current bandwidth limitat...

Which project template should I use for Visual C++ game development project?

c++ visual-studio

Which project template should I use for Visual C++ game development project? I am really new to Visual Studio and I am little bit confused with all these Windows Form Application, Win32 Console