JVM synchronization with Memory Management Aspects
The JVM organizes the data of a running Java application into several runtime data areas: one or more Java stacks, a heap, and a method area.
JVM is Allocating a Stack for each Thread with below in memory insights:
1. Local variables of methods.
2. Method parameters.
3. Return value of each method that invoked by this thread.
4. Only primitive data types and object references resided in Stack Memory, no object values allowed.
On the other hand, JVM has only one Heap that is shared by all thread:
Heap memory contains only object values and no primitive types or object references, even array of primitive types stored in heap memory because in Java Arrays considered as objects.
JVM also has another area which is called Method Area with below nature:
1. Method area contains Class variables (static Variables).
2. Like Stack it has only primitive types and object references.
3. Unlike stack it is shared between all threads like Heap Memory.
4. It has another part which is called runtime constant pool which is explained below.
5. It stores method data, and code for methods and constructors.
6. The method area is created on virtual machine start-up.
The following exceptional condition is associated with the method area:
If memory in the method area cannot be made available to satisfy an allocation request, the Java Virtual Machine throws an OutOfMemoryError.
Runtime Constant Pool:
Is per-class or per-interface representation of constant-pool table in class file (Compiled code or byte code).
Each run-time constant pool is allocated from the Java Virtual Machine's method area.
The run-time constant pool for a class or interface is constructed when the class or interface is created by the Java Virtual Machine.
So you can think of Runtime Constant pool is the Constant pool itself and below more details about constant pool:
It is generated for each class file in which it keeps all symbolic references in one place.Each class file has a constant pool, and each class or interface loaded by the JVM has an internal version of its constant pool called the runtime constant pool.
The runtime constant pool is an implementation-specific data structure that maps to the constant pool in the class file. In my opinion it is memory representation of constant pool per class file.
So if you think you got the idea here is another thing to know , keep reading there is a lot of fun coming :-)
What is symbolic references?
It is a byte code representation that refer to constant pool items. As you read above constant pool has symbolic references which is symbol table with references to class data which allow only below data types:
· numeric literals
· string literals
· class references
· field references
· method references
For example, the following code:
Object foo = new Object();
Would be written in byte code as follows:
0: new #2 // Class java/lang/Object1:
dup2: invokespecial #3 // Method java/ lang/Object "<init>"( ) V
The new opcode (operand code) is followed by the #2 operand. This operand is an index into the constant pool and therefore is referencing the second entry in the constant pool.
Sample of the constant pool in the generated class file would look like:
Constant pool:
#1 = Methodref #6.#17 // java/lang/Object."<init>":()V
#2 = Fieldref #18.#19 // java/lang/System.out:Ljava/io/PrintStream;
#3 = String #20 // "Hello"
#4 = Methodref #21.#22 // java/io/PrintStream.println:(Ljava/lang/String;)V
Synchronized Blocks in Instance Methods
You do not have to synchronize a whole method. Sometimes it is preferable to synchronize only part of a method. Java synchronized blocks inside methods makes this possible.Only one thread can access synchronized block at time.
Synchronized methods
It is like synchronized block in which it is only accessible by one thread at time.And the question that comes in mind when knowing that part is where are methods stored in memory to be accessible by other threads, here comes Method area. However different tutorials state method calling is tracked in Stack memory but it seems that there is another funny part which I will try to cover.
References:
https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html
http://blog.jamesdbloom.com/JVMInternals.html#constant_pool
http://tutorials.jenkov.com/java-concurrency/synchronized.html
Great efforts, keep going.
Good one