Fields of Class, Are They Stored in the Stack or Heap

Object methods stored in stack or heap?

Will explain based on how it works in Java.

Methods and variables(inside methods) are stored in the stack.

Local variables (variables inside methods) are stored in the stack. But not the method itself.

By method, we refer to the behaviour or the list of instructions that needs to be executed. This does not vary every method call and not even vary for every object instance created. The behaviour remains the same at the class level.

The behaviour is stored in a region called Method area. You can refer Java Spec for more details.

As per spec,

The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it. This version of the Java Virtual Machine specification does not mandate the location of the method area or the policies used to manage compiled code.

It is left to the JVM implementation on where the method area is located.

Implementations like HotSpot VM, until Java 7, used to store the method area as part of the heap. But from Java 8, it is moved out of heap and the space allocated for heap is not consumed by the method area.

What happens to for example the constructor?

Constructions are methods with a special name called, <init>.1. They are stored in the same way as other methods.

As a side note, there is a class initialization method, called <clint>, which handles static block in class.2

Struct has class reference then it will store on stack or heap C#

struct is a value type, and is therefore allocated on the stack

This is incorrect. A more correct statement would be "value types can be stored on the stack", see the truth about value types. The value type will probably be stored on the stack if given as a parameter, even if the jitter if free to store it wherever it damn pleases. But if the struct is part of a class, it will be stored on the heap, with all the other fields of the class.

Now, the reference to A will be stored as part of the struct, i.e. 8 bytes on x64. But the actual object it points to, including the int Value, will be stored on the heap.

How does java object with no of state fields get stored in JVM?

There will be an instance created in the heap, even if there are no fields; additionally there will be 2 headers for this instance created : mark and class. You are calling hello on an instance after all and the java language specification explicitly says that Object instances are created in the heap.

When the code runs enough times, JIT will kick in - at some point, it might prove that a certain instance might not be needed and might elide that allocation away. Or, if the instance is purely local and does not escape an optimization called scalar replacement might happen, when an instance could be dissolved into fields, and not allocated in the heap.

Does the Code Block of a method live in the stack or heap at the moment of execution?

Methods are not instantiated. Classes are instantiated in order to create objects.

Objects consist of data members and methods. Only the data members are allocated either somewhere in the process'es memory dynamically. The code of all methods is statically located in a section of memory called 'code segment'. No code of any method is ever copied. It is not needed since it's perfectly constant.

Stack has nothing to do with code. Only local variables and parameters live on the stack. Note that if the type of the variable / parameter is a reference, then just the value of the reference (pointer) lives on the stack, but the actual object it's pointing to is located somewhere else.

An article providing an introduction into .NET's memory management basics can be found here.

Note: This is a little bit simplifying view, but accurate for your level of knowledge.



Related Topics



Leave a reply



Submit