Memory space manipulating in Java(Section one:Basic Concepts in Java)

来源:互联网 发布:ff14敖龙族捏脸数据 编辑:程序博客网 时间:2024/05/26 08:41
 

HTML Tags and JavaScript tutorial



Memory space manipulating in Java(Section one:Basic Concepts in Java)





Introduction
 
After reading several articles on memory handling in Java in AIX, I decided to write something about this topic. For purposes of doing a summary for myself, also I hope it could be helpful to those who want to know it.
Basic Concepts in Java
  Before enter our main topic, we are going to review some basic concepts in Java.
Those who have been familiar with following concepts can skip this section.
Object
:
In world of Java, we know that everything is an
Object
(except for primitive type like int, double and so on).
Reference
:
In Java there is not a word named pointer, which should be very familiar to people who have been experienced of C/C++, to manipulate an object,
there is a new word
Reference
have been introduced, which also can be seen as an object, is used to help to manipulate the target object. It looks somewhat like a wrapper object of C/C++ pointer, as you can still remember you use *somePointerName to manipulate value of the target address in .
 
Class
:
Besides Reference, you know there must be something that used to define what the type of object is. The keyword
Class
, which is used to express the structure of the given Object, have used in most object-oriented languages. A
Class
is usually consists of two members: data elements & functionality, which also know as Fields & Methods.
A data member could be primitive data type, or a reference to a specified object. And functionality, also call method, means a way to do something.
 
Storage lives in Java
When you create an object with its reference, where they are being kept? Again, there may come to several terminologies.
1)
     
Register(s)
: Fastest storage because it exists directly inside the processor. They can be allocated by compiler, which instead, may have been created by using
Assembling or C language.
2)
     
Stack
: Creating objects on the stack is the most efficient way to allocate storage for objects and to free that storage. But with constraint of limited space and explicitly specifying size & lifecycle of the object. In Java, usually references to the Objects will be kept here. And those with primitive data type, also will be kept here, thus access to primitive data type normally can get a higher speed.
(To give size for a random object instance is impossible, but as to primitive type, Java determines the size of each primitive type and it would change from
one machine to another.)
3)
     
Heap
: In a sense you can treat JVM as a normal process running in an Operating System. There should be an area of memory allocated for the process used for dynamic memory allocation. When you create an object with
new
statement, you are asking JVM for allocating a space in Heap to place the object.
We will see it in more detail in the following section.
4)
     
Static storage
: “Static” is used here in the sense of “in a fixed location” (although it’s also in RAM). Static storage contains data that is available for the entire time a program is running.
Notice that here what does the word program mean? Each JVM will have its own static area which used as storage for static data. That mean your WebSphere Application Servers can’t run into each other’s static area as they run on different JVM, the same to your Java class start by running main(). (This becomes possible since JDK 5.0)
You can use the static keyword to specify that a particular element of an object is static, but Java objects themselves are never placed in static storage.
5)
     
Constant storage
: Constant values are often placed directly in the program code, which is safe since they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM), in embedded systems.
6)  
Non-RAM storage
: If data lives completely outside a program, it can exist while the   program is not running, outside the control of the program. The two primary examples of this are streamed objects, in which objects are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects are placed on disk so they will hold their state even when the program is terminated. The trick with these types of storage is turning the objects into something that can exist on the other medium, and yet can be resurrected into a regular RAM-based object when necessary. Java provides support for lightweight persistence, and future versions of Java might provide more complete solutions for persistence.
Run your Java code
  We know Java is one of intercept programming languages, which means it can not be executed directly until being translated into instruction code by correspondent interceptor. The following steps simply show what need to be done to make your Java code executable.
1)
     
Use
javac
to compile your java source into bytecodes. This will result in .class file which contains data & functionality elements of the java source, being created.
2)
     
Use
java
to execute your java bytecodes. This action in fact, will firstly compile the bytecodes into native machine code (instruction code) that can be recognized & execute by hardware. In normal case, the final compiled instruction code will consist of data & text; this should be the same as C/C++, even Assembling Language.
         Note that JIT plays an important role here. I will simply explain it with the following 
         picture:
        
   
The JIT Compiler comes into use when a Java method is called; it compiles the bytecodes of
that method into native machine code, compiling it

just in time

to execute. When a method has been compiled 
 
(The JVM interprets a method until its call count exceeds a JIT threshold)
, the JVM calls the compiled code of that method directly instead of interpreting it.
If you want to know more about Java, I would suggeset you to read Thinking In Java Third Edition, as in this section I have referred many from there.