0

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)

Yerendi
  • 21
  • 4

1 Answers1

2

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Boxing and unboxing will only take place if the object in question is a value type. – Servy Oct 17 '17 at 13:23
  • 1
    @Servy Yes, but we don't know what's being passed to that function.OP did not show this in the question. – Programmer Oct 17 '17 at 13:24
  • And yet you said that the value is *always* boxed, and *always* unboxed, which is simply wrong. If you feel that the question does not contain enough information to post a correct answer then *don't post an answer* until the question can be clarified enough to be answerable. – Servy Oct 17 '17 at 13:25
  • Thanks, I can't upvote your answer but your suspicion is probably right because 'object[] args' contains Objects. – Yerendi Oct 17 '17 at 13:25
  • @Servy If I need more info I would comment. I have a feeling that's what the issue is since I've ran into that issue before and that seems to be the only issue unless `Stream.WriteObject` is doing something we don't know what. – Programmer Oct 17 '17 at 13:28
  • @Programmer So you have no idea whatsoever what the problem is, because you realize that the question is so lacking in information that the problem could be literally anything, have no basis whatsoever for asserting an answer is correct, because of all of that missing information, have posted provably incorrect statements in that answer, but you have no interest in actually figuring out what the actual problem in the question is because...why? – Servy Oct 17 '17 at 13:30
  • 1
    @Servy I was not notified for your last comment. Note that I didn't say *"always"* like you mentioned in your comment. I simply missed to mention *value types* which I have added but that did not mean *"always"*. What's in that question code+screenshot log is enough for me to answer that. Read below: – Programmer Oct 17 '17 at 17:01
  • @Servy As for for my *"incorrect answer"* or *"question lacking information"* (according to you), please take a look at the screenshot OP uploading in the question. According to that image, `WriteArgs` allocates 11.4KB while `WriteObject` allocates 9.4KB. Since `WriteObject` is inside `WriteArgs`, this means that `WriteArgs` really allocates 2KB alone. The extra 2KB is the result of boxing. I've been using process of elimination to answer questions most of time here and they are correct 90 percent of time. This removes the unnecessary back and forth comments and saves my time. – Programmer Oct 17 '17 at 17:02
  • @Programmer You said that calling the function will result in boxing. You didn't say, "on very specific situations, namely where the value provided is a value type, calling the function can result in boxing" Saying that it *will* result in boxing when it won't, is simply wrong. And the question is lacking sufficent information to be answered [according to you](https://stackoverflow.com/questions/46789382/gc-alloc-for-unknown-reason/46791036?noredirect=1#comment80528940_46791036), not just me. *You* think that the question isn't answerable, and yet posted an answer anyway. – Servy Oct 17 '17 at 17:04
  • @Servy There are some questions you can solve without some few missing info by using the already available ones + process of elimination. That comment you linked was a reply to your own comment to tell you that even for what you said, it could be the issue I mentioned because that's not mentioned by OP. *But then my last comment just explained how I came up with that.* If you can't read and comprehend what's in my last comment then I think you probably don't care about what's in this answer and just wanted to leave a comment. – Programmer Oct 17 '17 at 17:14
  • Yes, you are right when you said "on very specific situations," but that's not the only thing you complained about. You complained about helping someone that asked a question you considered to be "missing details/incomplete" and also about me having *"no interest in actually figuring out what the actual problem in the question"*. I didn't know that viewing images on the question, carefully reading the question and reasoning why the mentioned problem == "having no interest". Happy coding!. – Programmer Oct 17 '17 at 17:19
  • @Programmer I told you that you shouldn't be answering a question that *you have stated yourself* that you don't know the answer to, and that you feel hasn't provided enough information to be answered. If you want to help, then *help the improve the question such that it's actually answerable*, rather than posting factually incorrect answers to questions that don't have enough information to be answered. When the question actually contains enough information to actually be answered in the first place, *then* you can provide an answer, and actually have it be correct at that. – Servy Oct 17 '17 at 17:21