Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the method representation in memory?

Tags:

java

c#

memory

jvm

clr

While thinking a little bit about programming in Java/C# I wondered about how methods which belong to objects are represented in memory and how this fact does concern multi threading.

  1. Is a method instantiated for each object in memory seperately or do all objects of the same type share one instance of the method?
  2. If the latter, how does the executing thread know which object's attributes to use?
  3. Is it possible to modify the code of a method in C# with reflection for one, and only one object of many objects of the same type?
  4. Is a static method which does not use class attributes always thread safe?

I tried to make up my mind about these questions, but I'm very unsure about their answers.

like image 460
Emiswelt Avatar asked Nov 20 '11 20:11

Emiswelt


People also ask

What is the representation of memory?

The memory representation of a text is the result of inferential processes that take place during reading. These processes allow the reader to establish coherence as he or she proceeds through the text.

How are methods stored in memory?

Methods are stored somewhere else in the memory. Notice that methods are per-class, not per-instance. So typically, the number of methods doesn't change over the run-time of a program (there are exceptions). In traditional models, the place where the methods live is called the "code segment".

What is memory representation in computer architecture?

The computer memory stores different kinds of data like input data, output data, intermediate results, etc., and the instructions. Binary digit or bit is the basic unit of memory. A bit is a single binary digit, i.e., 0 or 1. A bit is the smallest unit of representation of data in a computer.

How are Java methods stored in memory?

Static information (interface & class boxes) and instance information (object boxes) are stored in the heap. Method information is stored in the run-time stack.


2 Answers

Each method in your source code (in Java, C#, C++, Pascal, I think every OO and procedural language...) has only one copy in binaries and in memory.

Multiple instances of one object have separate fields but all share the same method code. Technically there is a procedure that takes a hidden this parameter to provide an illusion of executing a method on an object. In reality you are calling a procedure and passing structure (a bag of fields) to it along with other parameters. Here is a simple Java object and more-or-less equivalent pseudo-C code:

class Foo {   private int x;    int mulBy(int y) {     return x * y   } }  Foo foo = new Foo() foo.mulBy(3) 

is translated to this pseude-C code (the encapsulation is forced by the compiler and runtime/VM):

struct Foo {     int x = 0; }  int Foo_mulBy(Foo *this, int y) {     return this->x * y; }  Foo* foo = new Foo(); Foo_mulBy(foo, 3) 

You have to draw a difference between code and local variables and parameters it operates on (the data). Data is stored on call stack, local to each thread. Code can be executed by multiple threads, each thread has its own copy of instruction pointer (place in the method it currently executes). Also because this is a parameter, it is thread-local, so each thread can operate on a different object concurrently, even though it runs the same code.

That being said you cannot modify a method of only one instance because the method code is shared among all instances.

like image 192
Tomasz Nurkiewicz Avatar answered Oct 05 '22 23:10

Tomasz Nurkiewicz


The Java specifications don't dictate how to do memory layout, and different implementations can do whatever they like, providing it meets the spec where it matters.

Having said that, the mainstream Oracle JVM (HotSpot) works off of things called oops - Ordinary Object Pointers. These consist of two words of header followed by the data which comprises the instance member fields (stored inline for primitive types, and as pointers for reference member fields).

One of the two header words - the class word - is a pointer to a klassOop. This is a special type of oop which holds pointers to the instance methods of the class (basically, the Java equivalent of a C++ vtable). The klassOop is kind-of a VM-level representation of the Class object corresponding to the Java type.

If you're curious about the low-level detail, you can find out a lot more by looking in the OpenJDK source for the definition of some of the oop types (klassOop is a good place to start).

tl;dr Java holds one blob of code for each method of each type. The blobs of code are shared among each instance of the type, and hidden this pointers are used to know which instance's members to use.

like image 24
kittylyst Avatar answered Oct 06 '22 00:10

kittylyst