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.

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值