2

I am doing a service app which will run continuously for that i need to take care of memory usage in my code.

I am using Stopwatch class to calculate the code performance,

for e.g) Return value vs Out parameter - i just calculate this using for () loop with higher iteration count and found that Out parameter took less time than Returning value from method.

            int Iterations = 10000000;
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i < Iterations; i++)
            {
                BigStruct bs = ReturnValue();
                total += bs.dec1;
            }
            sw.Stop();
            Console.WriteLine("Using return value: {0}",
                              sw.ElapsedMilliseconds);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < Iterations; i++)
            {
                BigStruct bs;
                OutParameter(out bs);
                total += bs.dec1;
            }
      `Console.WriteLin`e("Using out parameter: {0}",sw.ElapsedMilliseconds);

Similarly, I want to calculate memory usage of my code how much memory is taking while passing bitmaps array to another method as value type argument. so i can find out the high memory usage and change the my code accordingly.

So this will highly help me to find out the best way instead of googling for all..

Thanks.

Abu Muhammad
  • 421
  • 2
  • 13
  • @Mitch wheat, That's for getting full process memory usage , i know that before. but what i wanting to do here is , calculating. finding out memory usage of particular code snippets. – Abu Muhammad Nov 16 '16 at 05:53
  • @Wheat, But it will not show correct memory usage, Is there any debugging tools available to achieve the same? – Abu Muhammad Nov 16 '16 at 05:59
  • 1
    VS2015 diagnostic tools have heap profiler. It's not exactly what you want, but I don't think you can do much better than heap analysis in GCed environment – slawekwin Nov 16 '16 at 07:31

1 Answers1

0

Memory consumption in systems with automatic memory management (Garbage Collection).

In one case application can produce much not handled objects and GC do not release them. In other case GC can release memory aggressively. So GC should be taken into consideration as active part.

int[] gccounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i &lt;= GC.MaxGeneration; i++)
    gccounts[i] = GC.CollectionCount(i);
Lucifer
  • 1,594
  • 2
  • 18
  • 32
  • I didnt get you properly. Pls explain – Abu Muhammad Nov 16 '16 at 06:08
  • You can see [here](http://www.codeproject.com/Questions/1098890/How-do-I-measure-memory-usage-of-my-application-in) for more – Lucifer Nov 16 '16 at 06:15
  • Actually my case is to be, read the dataset from DB and extract the same and convert svg files into image then need to send mail to the recipient. For this I have 4 more methods and passing struct and dataset, bitmap arrays to each of method. Since its a full time service app , i need to take care of memory and performance . also the above code running in high priority thread. if i put it altogether in single method , then it might look ugly and not fit into the coding standard. pls advice – Abu Muhammad Nov 16 '16 at 06:15
  • sorry i am also a new comer i am performing c# past 3 months since i had already performed memory finding program but I haven't gone this deep – Lucifer Nov 16 '16 at 06:32
  • ok thanks for your time. – Abu Muhammad Nov 16 '16 at 06:38
  • was this answer helpfull – Lucifer Nov 25 '16 at 11:32