What's the Method Representation in Memory

What's the method representation in memory?

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.

Object representation in JVM

First of all, the quoted text says "In some of Oracle’s implementations of the Java Virtual Machine ...". This description is not correct for all implementations.

It think your misunderstanding is based on a mis-parsing of the text. What I think it is actually saying is this:

  1. A handle is a pointer to a pair of pointers.
  2. The first pointer of the pair points to a table.
  3. The table contains the methods of the object.
  4. The table also contains a pointer to the Class object.
  5. The second pointer in the pair points to the memory area that holds the object data.

The handle has two pointers, not three.


My understanding is that in modern Oracle JVMs, the representation of a handle is as follows:

  1. A handle is a pointer to an node in the heap.
  2. The node (for an ordinary object) consists of two header words followed by zero or more words that hold the object data.
  3. The two header words consist of the following:

    • "klass" word containing an index into the JVM's table of class descriptors
    • a flag word contains bits used for various purposes: e.g. GC "mark" bits, bits to represent the object's primitive-lock state, bits related to identity hashcodes, the object's GC age and so on.
  4. The class descriptor includes the method table and the reference to the Class object. It also gives the size (in words) of an instance's data area that the GC needs.

The representation for arrays is a bit different. For a start, there is a 3rd word containing the array length.

(See also What is in java object header)


But note that these details are implementation specific. Particularly the flag word.

How many methods in a class will be created in memory when an object is created in C#?

None.

The methods are not created in memory when you instantiate an object, only the fields and properties are.

The assembly that contains the methods is loaded whenever any part of the assembly is referenced.

Methods get compiled into memory only when they are called. The JIT (Just-In-Time compiler) turns the methods from IL into machine code at that moment in time.

Difference between internal memory representation of static methods and instance methods

This is completely implementation-dependent, but generally speaking, there's no fundamental difference between the in-memory representation of static and nonstatic methods. Internally, they're just executable (byte)code, which has the same representation regardless of whether the method has a receiver object.

Hope this helps!

Does having more methods in a class mean that object uses more memory at runtime

The in-memory representation of an instance of a class is mainly just its internal state plus a pointer to an in-memory representation of the class itself. The internal representation of an instance method has one more argument than you specified in the class definition - the implicit this reference. This is how we can store only one copy of the instance method, rather than a new copy for every instance.

So a class with more methods will take up more memory than a class with less methods (the code has to go somewhere), but an instance of a class with more methods will use the same amount of memory, assuming the classes have the same data members.

Execution time will not be affected by the number of other methods in the class.

Best way to represent text in memory

If the derived classes represent the data very differently, don't implement a common Save method for them, Those classes knows best how to Save their data.
Make Save() abstract and have each of the subclass implement the saving.

There might be something in common for doing a Save() (e.g. opening the actual file, error handling). So have your base class provide a Save() method that's responsible for that which in turn calls a virtual Save(System.IO.TextWriter writer); method that each of your subclasses implement.

Where is information about methods of Java objects kept?

The code for methods isn't duplicated for all the instances, that would be completely unnecessary. The code lives in a special area in the memory, and it's shared by all the instances. The memory required by instance variables on the other hand naturally is owned by every instance.

As to how a method is called, the object doesn't really need to ask the class every time it calls a method, it has a pointer to the method's code and can just call it right away.

For more information on the inner workings of the JVM, refer here: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html

C# Object Memory Representation

Short answer - there will be no extra memory per object to store a default value
A read only property with only a return statement will compile to a method

get_DefaultValue()


Related Topics



Leave a reply



Submit