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

原创 2007年09月23日 14:09:00
 

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.
        


相关文章推荐

In-memory Computing with SAP HANA读书笔记 - 第一章:Basic concepts of in-memory

本文为In-memory Computing with SAP HANA on Lenovo X6 Systems第一章Basic concepts of in-memory computing的读书...

读书笔记-HBase in Action-第二部分Advanced concepts-(3)非Java客户端

HBase Shell HBase shell使用起来最方便,进入HBase shell控制台即可使用。 $ $HBASE_HOME/bin/hbase shell 常见操作有create(创建表)/...
  • cargogo
  • cargogo
  • 2014年07月03日 13:07
  • 1586

概率论基础概念总结 Basic Concepts in Statistics

在这里自己总结一些概率论中常用的基本概念,方便以后参考。这里强调对概念的理解,不强调精确的定义和理论。 大数定律 Law of Large Number LLN 中心极限定理 Central Limi...
  • dymodi
  • dymodi
  • 2016年11月08日 05:30
  • 913

Exception in thread "http-bio-8080-exec-10" java.lang.OutOfMemoryError: PermGen space

Exception in thread "http-bio-8080-exec-10" java.lang.OutOfMemoryError: PermGen space at java.lang....

Exception in thread "http-apr-8080-exec-6" java.lang.OutOfMemoryError: PermGen space 解决!

在网上找了半天,也在apache目录上 bin/catalina.bat文件中的echo Using CATALINA_BASE: "%CATALINA_BASE%" 上面加入了代码: -XX...

Basic RPCs in Node.js with a Java Backend

The original: http://andrewbrobinson.com/2012/03/28/basic-rpcs-in-node-js-with-a-java-backend/ ...
  • flary
  • flary
  • 2012年05月20日 09:03
  • 967

[转载] Basic try-catch-finally Exception Handling in Java

此文章转载自 Exception handling in Java Table of Contents The Call Stack ExplainedThrowing Except...

JAVA ONE 2013 in San Francisco 注定是javaFX爆起的一年

JAVA ONE  2013  in San Francisco  注定是javaFX爆起的一年。            一年一度的JAVA NOE 大会将在旧金山举行。javaFX已出世多年,但总...

Out Of Memory Issues in java

Memos for Java Out Of Memory Issues * Exception in thread “CompilerThread1″ java.lang.OutOfMemoryEr...
  • lzy_168
  • lzy_168
  • 2012年05月22日 14:42
  • 1634

Hunting Memory Leaks in Java

Inexperienced programmers often think that Java’s automatic garbage collection completely frees them...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Memory space manipulating in Java(Section one:Basic Concepts in Java)
举报原因:
原因补充:

(最多只允许输入30个字)