-2

Suppose below code:

Class test
{
   void foo()
   {
      int i=0;
   }
}

Test t=new Test();
// if many threads call t.foo();

Does each thread has its own memory for calling foo? It means each thread has it's own i?

How does Java/C# allocate memory when it calls the function? As I remember, in C#, each thread will be allocated 1M memory. What about Java?

roast_soul
  • 3,554
  • 7
  • 36
  • 73

2 Answers2

0

There is essentially no difference in memory allocation between single threaded and multithreaded applications (at least in .Net/Windows world).

Memory allocated either in the heap (normal objects) or on the stack (for local variable/local struct/function parameters). Default stack size for C# applications (and most Windows application) is 1Mb per thread, but heap is shared between all threads.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

What will happen in the scenario you describe for C# (not sure about Java but I believe it will behave similar) is that the value i will be stored in short term storage (either the stack or register depending on what the JIT wants to do). This is because it is a value type. If it was a reference type, then it would most likely go on the heap.

Each time the function is called (regardless of thread) the function will get a new instance of the variable i. So it doesn't matter which thread, or how many threads, only how many calls to the function are made.

One thing to note is that you can't always guarantee where something will be allocated, and for the most part you shouldn't care. The JIT/CLR is allowed to do whatever it wants so long as it doesn't affect the single threaded view of the events and the functionality of the program (there are edge cases, but for 99% of code this statement is correct).

You can also read Eric Lippert's answer to this question (Fields of class, are they stored in the stack or heap?) for a better understanding as well as his blog (http://blogs.msdn.com/b/ericlippert/) and (http://ericlippert.com/) he's discussed this a number of times in greater detail.

Community
  • 1
  • 1
Zipper
  • 7,034
  • 8
  • 49
  • 66