Memory issue for a small program

Hello All,
I have a simple code
using System;

class Program
    public static void Main(string[] args)
        int N = Convert.ToInt32(Console.ReadLine());
        long sum =1;
        for(int i =1; i<= N; i++)
            sum *= i;
        Console.WriteLine("{0}", sum);

Open in new window

When I run this it works just fine, but when I look at the memory consumed, if I pass the input as 8 it has taken 320kb of memory!
Not sure what causes that specific input to grow that memory!
attached the screen shot of HackerEarth results.

Screenshot of Results
Input1 was 5
Input2 was 7
Input3 was 8
Inpu4 was 10

Need help in understanding what could be causing that memory increase?

Thanks in Advance
Who is Participating?
Miguel OzConnect With a Mentor Software EngineerCommented:
IF you run your code in VS, it shows no memory issues. My suggestion is to contact Hacker Earth and find out how they run your code, it could be possible that they do not clean up memory(after all Garbage Collector is non deterministic and could run any time) of the previous run as fast as run your programs.
i assume you are running the program in debug mode. if that is true, 320 kb is not much and most of it is due to the debugger creating some extra memory. heap and stack memory was not allocated bytewise or in kilobytes but in chunks of bigger sizes. so it is well possible that 64 KB is the minimum chunk size and 320 is the next size if 64 KB is not sufficient.

if you can't reproduce the results that an input of 8 always consumes 320 KB and values 5, 7, 10 have 64 KB only, you may forget the above. it simply could be due to some timeouts that caused the debugger to save its current status and create a new greater debug space.

if an input of 8 always causes significant more memory to be consumed, it is more strange. perhaps it is dependent on how many loop cycles are to be performed. you may test if you get the same results for 4, 16, or 32 as well and post the results.

yamurthyAuthor Commented:
Yes I checked with HackerEarth and it seems to be some issue that side. They just passed on the link for me to read! When I resubmitted, this time some other input caused more memory and not the input value 8.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.