How does Java/C# allocate memory when multiple threads call same functions
NickName:roast_soul Ask DateTime:2013-03-15T12:06:24

How does Java/C# allocate memory when multiple threads call same functions

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?

Copyright Notice:Content Author:「roast_soul」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15424542/how-does-java-c-allocate-memory-when-multiple-threads-call-same-functions

Answers
Alexei Levenkov 2013-03-15T04:13:16

There is essentially no difference in memory allocation between single threaded and multithreaded applications (at least in .Net/Windows world).\n\nMemory 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. ",


More about “How does Java/C# allocate memory when multiple threads call same functions” related questions

How does the synchronize functionality work in java?

Since I've started programming in Java I have been wondering this (about a year or two). In C, we must know the different method to correctly avoid deadlock between thread and thus there is much more

Show Detail

why eclipse does not automatically resolve all includes in C as it does in java?

i am trying to fix my eclipse IDE for C, so that it should resolve all include issues automatically as it does in java (import). I have included my compiler include directory under project->propert...

Show Detail

How to run C code in Java

I have found Java to be an excellent high level language but it lacks some of the speed and flexibility of its founding language C. Seeing the Android OS was written in C and Java it seems likely ...

Show Detail

Which JRE does C:\ProgramData\Oracle\Java\javapath\java.exe use?

I'm trying to figure out which environment variable java uses to find/detect the JRE used by C:\ProgramData\Oracle\Java\javapath\java.exe. As per Oracle's design, the only files I have in the C:\

Show Detail

How does Java compute the sine and cosine functions?

How does Java find sine and cosine? I’m working on trying to make a game that is a simple platformer something like super Mario or Castlevania. I attempted to make a method that would rotate an ima...

Show Detail

How does Java/C# allocate memory when multiple threads call same functions

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 t...

Show Detail

Threading: does c# have an equivalent of the Java Runnable interface?

Does c# have an equivalent of the Java Runnable interface? If not how could this be implemented or is it simply not needed? thanks.

Show Detail

Does Java have the equivalent of @synthesize in Objective-C?

I am learning Obj-C and discovered the @synthesize directive which generates accessor and mutator methods. Groovy auto-generates getters/setters but I'm not sure if Java does. Does Java have a si...

Show Detail

How does Java concatenate 2 strings?

Why does the following print 197, but not 'bc'? System.out.println('b' + 'c'); Can someone explain how to do proper concatenation on Java? P.S. I learnt some Python, and now transforming to lea

Show Detail

How does the Java Virtual Machine handle I/O operations?

I was looking through the list of Java bytecode instructions and I noticed that there aren't any I/O instructions. That intrigued me. How does the JVM execute methods like System.out.println when it

Show Detail