Yerendi
Yerendi

Reputation: 21

GC Alloc for unknown reason

I have problem with this:

 public void WriteArgs(object[] args)
    {
        for (var i = 0; i < args.Length; i++)
        {
            Stream.WriteObject(args[i]);
        }
    }

It's allocating 2 KB on 50 calls (screen). How is that possible? Which part of this method is allocating memory? (On this screen you san see that Stream.WriteObject(args[i]); is allocating 9.4 so 2KB is allocating by WriteArgs itself)

Upvotes: 0

Views: 742

Answers (1)

Programmer
Programmer

Reputation: 125275

One big suspect is the object in your object[] args parameter. This looks like a boxing and unboxing issue.

When you call the WriteArgs function and pass something to it, it will perform boxing to convert that parameter to object.


Not sure what the Stream.WriteObject is or where you got that but if it takes object as parameter too and when it enter inside the WriteArgs function, it will also perform unboxing in order to use the variable that passed to it. Both boxing and unboxing allocate memory.

FIX:

Remove object[] args and create multiple overloads for your WriteArgs function that can handle different Object types as parameter.

Also, do the-same for the Stream.WriteObject if you wrote it or find alternative function for it. You can read more about boxing and unboxing here.

EDIT:

The boxing and unboxing issue described above only apply to Value Types. If you are already passing Reference Type to that function then the only problem here is the Stream.WriteObject function.

Upvotes: 2

Related Questions