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
Anjeneya MurthySr. ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

Anjeneya MurthySr. ConsultantAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.