JVM

Analysis & Design

  1. The object-oriented paradigm is a new and different way of thingking about programming.
  2. Most of the analysis and design methodologies are intended to sovle the largest of problems. Remember that most projects don’t fit intot hat category, so you can usually have successful analysis and design with a relatively small subset of what a methodology recomments.
  3. It is also easy to get stuck, to fall into “analysis paralysis”, where you feel like you can't move forward because you haven’t nailed down every little detail ath the current stage. Remember, no matter how much analysis you do, there are some things about a system that won’t reveal themselves until design time, and more things that won’t reveal themselves until you are coding, or not even until a program is up and running.
  4. Phase 0: Make a plan.  Having a few milestones along the way helps to focus and galvanize your efforts around those milestones instead of being stuck with the single goal of “finish the project”. In addition, it divides the project into more bite-sized pieces and makes it seem less threatening.
  5. The high concept is quite important because it sets the tone for your project; it is a mission statement. You will not necessarily get it right the first time.
  6. Phase 1: What are we making?  The system specification is a top-level exploration into the problem and in some sense a discovery of whether it can be done and how long it will take.
  7. It is necessary to stay focused on the heart of what you are trying to accomplish in this phase: Determine what the system is supposed to do.

Garbage Collector

  1. C++将自己的主要精力放在编译期间“静态”发生的所有事情static binding in compiling time,所以程序的运行期版本非常短小和快速. 在Java中,所有对象(exclude built-in type) 都必须在内存“堆”里创建. Java is a kind of pure object-oriented programming. All objects are created in heap by JVM. In C++, only the object created with new operation is allocated in heap. All the rest is settled down in stack at compiling time, along with fast speed in run time.
  2. 而在C++中,对象是在堆栈 stack中创建的。这样可达到更快的速度,因为当我们进入一个特定的作用域时,堆栈指针会向下移动一个单位,为那个作用域内创建的、以堆栈为基础的所有对象分配存储空间。而当我们离开作用域的时候(调用完毕所有局部构建器后),堆栈指针会向上移动一个单位。然而,在C++里创建“内存堆”(Heap)对象通常会慢得多,因为它建立在C的内存堆memroy heap基础上。这种内存堆实际是一个大的内存池,要求必须进行再循环(再生)。在C++里调用delete以后,释放的内存会在堆里留下一个洞,所以再调用new的时候,存储分配机制必须进行某种形式的搜索,使对象的存储与堆内任何现成的洞相配,否则就会很快用光堆的存储空间。之所以内存堆的分配会在C++里对性能造成如此重大的性能影响,对可用内存的搜索正是一个重要的原因。所以创建基于堆栈stack的对象要快得多。
  3. 在某些JVM里 where memory is allocated, file operation, IO, network is, and so on,Java堆的工作方式却是颇有不同的。它更象一条传送带:每次分配了一个新对象后,都会朝前移动。这意味着对象存储空间的分配可以达到非常快的速度。“堆指针”简单地向前移至处女地,所以它与C++的堆栈分配方式几乎是完全相同的(当然,在数据记录上会多花一些开销,但要比搜索存储空间快多了)。
  4. 它在收集“垃圾”的同时,也负责压缩堆里的所有对象,将“堆指针”移至尽可能靠近传送带开头的地方,远离发生(内存)分页错误的地点。垃圾收集器会重新安排所有东西,使其成为一个高速、无限自由的堆模型,同时游刃有余地分配存储空间。
  5. 为真正掌握它的工作原理,我们首先需要理解不同垃圾收集器(GC)Garbage Collection 采取的工作方案。一种简单、但速度较慢的GC技术是引用计数(Reference Count), it seems smart_ptr used often in C++。这意味着每个对象都包含了一个引用计数器。每当一个句柄同一个对象连接起来时,引用计数器就会增值。每当一个句柄超出自己的作用域,或者设为null时,引用计数就会减值。这样一来,只要程序处于运行状态,就需要连续进行引用计数管理——尽管这种管理本身的开销比较少。垃圾收集器会在整个对象列表中移动巡视,一旦它发现其中一个引用计数成为0,就释放它占据的存储空间。但这样做也有一个缺点:若对象相互之间进行循环引用,那么即使引用计数不是0,仍有可能属于应收掉的“垃圾”。为了找出这种自引用的组,要求垃圾收集器进行大量额外的工作。引用计数属于垃圾收集的一种类型,但它看起来并不适合在所有JVM方案中采用。The time when GC runs is not decided.
  6. 垃圾收集并不建立在引用计数的基础上。相反,它们基于这样一个原理:所有非死锁的对象最终都肯定能回溯至一个句柄,该句柄要么存在于堆栈中,要么存在于静态存储空间。这个回溯链可能经历了几层对象。所以,如果从堆栈和静态存储区域开始,并经历所有句柄,就能找出所有活动的对象。对于自己找到的每个句柄,都必须跟踪到它指向的那个对象,然后跟随那个对象中的所有句柄,“跟踪追击”到它们指向的对象……等等,直到遍历了从堆栈或静态存储区域中的句柄发起的整个链接网路为止。中途移经的每个对象都必须仍处于活动状态。注意对于那些特殊的自引用组,并不会出现前述的问题。由于它们根本找不到,所以会自动当作垃圾处理。
  7. JVM还采用了其他许多加速方案。其中一个特别重要的涉及装载器以及JIT编译器。若必须装载一个类(通常是我们首次想创建那个类的一个对象时),会找到.class文件,并将那个类的字节码送入内存。此时,一个方法是用JIT编译所有代码,但这样做有两方面的缺点:它会花更多的时间,若与程序的运行时间综合考虑,编译时间还有可能更长;而且它增大了执行文件的长度(字节码比扩展过的JIT代码精简得多),这有可能造成内存页交换,从而显著放慢一个程序的执行速度。另一种替代办法是:除非确有必要,否则不经JIT编译。这样一来,那些根本不会执行的代码就可能永远得不到JIT的编译。JIT = Just in time.
  8. Java has a garbage collector, which looks at all the objects that were created with new and figures out which ones are not being referenced anymore. Then it releases the memory for those objects, so the memory can be used for new objects. This means that you never need to worry about reclaiming memory yourself. You simply create objects, and when you no longer need them, they will go away by themselves. This eliminates a certain class of programming problem: the so-called “memory leak,” in which a programmer forgets to release memory.
  9. Java has the garbage collector to reclaim the memory of objects that are no longer used.
  10. Now consider an unusual case: suppose your object allocates “special” memory without using new. The garbage collector only knows how to release memory allocated with new, so it won’t know how to release the object’s “special” memory. To handle this case, Java provides a method called finalize( ) that you can define for your class. Here’s how it’s supposed to work. When the garbage collector is ready to release the storage used for your object, it will first call finalize( ), and only on the next garbage-collection pass will it reclaim the object’s memory. So if you choose to use finalize( ), it gives you the ability to perform some important cleanup at the time of garbage collection.
  11. whereas in Java, objects do not always get garbage collected. Or, put another way: 
    1. 1. Your objects might not get garbage collected.
    2. 2. Garbage collection is not destruction.
    3. 3. Garbage collection is only about memory.
  12. What it means is that if there is some activity that must be performed before you no longer need an object, you must perform that activity yourself.
  13. To clean up an object, the user of that object must call a cleanup method at the point the cleanup is desired. This sounds pretty straightforward, but it collides a bit with the C++ concept of the destructor. In C++, all objects are destroyed. Or rather, all objects should be destroyed. If the C++ object is created as a local (i.e., on the stack—not possible in Java), then the destruction happens at the closing curly brace of the scope in which the object was created. If the object was created using new (like in Java), the destructor is called when the programmer calls the C++ operator delete (which doesn’t exist in Java).
  14. In contrast, Java doesn’t allow you to create local objects—you must always use new.
  15. If you want some kind of cleanup performed other than storage release, you must still explicitly call an appropriate method in Java, which is the equivalent of a C++ destructor without the convenience.
  16. So it appears that finalize( ) is only useful for obscure memory cleanup that most programmers will never use. However, there is a very interesting use of finalize( ) that does not rely on it being called every time. This is the verification of the termination condition of an object.
  17. In some JVMs, the Java heap is quite different; it’s more like a conveyor belt that moves forward every time you allocate a new object. This means that object storage allocation is remarkably rapid. The “heap pointer” is simply moved forward into virgin territory, so it’s effectively the same as C++’s stack allocation. (Of course, there’s a little extra overhead for bookkeeping, but it’s nothing like searching for storage.)
  18. A simple but slow garbage collection technique is is called reference counting. This means that each object contains a reference counter, and every time a reference is attached to an object, the reference count is increased. Every time a reference goes out of scope or is set to null, the reference count is decreased.
  19. In faster schemes, garbage collection is not based on reference counting. Instead, it is based on the idea that any nondead object must ultimately be traceable back to a reference that lives either on the stack or in static storage.

Java Knowledge series 2的更多相关文章

  1. Java Knowledge series 4

    JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...

  2. Java Knowledge series 7

    Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...

  3. Java Knowledge series 5

    Interface from user, not from implementor.(DIP) Interface-Oriented Programming. Interface or Abstrac ...

  4. Java Knowledge series 3

    JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...

  5. Java Knowledge series 1

    Programming language evolves always along with Compiler's evolvement JVM as Additional Indirection I ...

  6. Java SE series:2. enhance your java basis! [doc chm: jdk6api Chinese reference]

    1. javaee(Web) and Android 2. how to use eclipse and break point debuging in eclipse, as to java web ...

  7. Java SE series:1. environment configure and Hello world! [We use compiler and packager to create an application!]

    1. cli (command line interface) and gui (graphic user interface) use javahome path, search classpath ...

  8. C++ Knowledge series 1

    Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...

  9. C++ Knowledge series STL & Const

    Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...

随机推荐

  1. poj3321(dfs序+树状数组)

    题目链接:https://vjudge.net/problem/POJ-3321 题意:给一个普通树(不是二叉树),并且已经编号,每个结点为1或0,有两种操作,对单个结点修改和查询一个结点的子树的所有 ...

  2. Qt 学习之路 2(7):MainWindow 简介

    Qt 学习之路 2(7):MainWindow 简介  豆子  2012年8月29日  Qt 学习之路 2  29条评论 前面一篇大致介绍了 Qt 各个模块的相关内容,目的是对 Qt 框架有一个高屋建 ...

  3. react 中文文档案例二 (头像时间)

    import React from 'react'; import ReactDOM from 'react-dom'; function formatDate(date) { return date ...

  4. C语言利用指针排序与选择排序算法

    //读入字符串,并排序字符串 #include <stdio.h> #include <string.h> #define SIZE 81 #define LIM 20 #de ...

  5. C语言之基本编程思想与基本概念扫盲

    前言:C语言是包含了很多编程的基本思想,理解C能够有助于理解其他高级语言,深刻理解编程很多基本思想:这对新手入门是有很多好处的,正所谓磨刀不误砍柴工,内功与基础修炼扎实了,才能开始盖高楼大厦. 这篇文 ...

  6. linux磁盘与文件管理

    一.硬盘的组成与分区 1.物理组成 *圆形的盘片(主要记录数据的部分) *机械手臂与机械手臂上的磁头(可读写盘片上的数据) *主轴马达,可以转动盘片,让机械手臂的磁头在盘片上写数据. *扇区为最小的物 ...

  7. nginx配置应用

    启动nginxvim /usr/local/lnmp/nginx/conf/nginx.conf mkdir /wwwcd /wwwvim index.html www.westos.orgmkdir ...

  8. 提取SQL中用到的表

    dos2unix * for i in `ls` do :}` awk '{print tolower($0)}' "${i}"|grep -Eiw "from" ...

  9. STL之set(唯一且有顺序)

    set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据, 在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序.应该注意的是set中数元素的值不能直接被改变. ...

  10. thinkPHP5.0分页传参

    分页函数paginate(),主要参数有:list_rows每页数量.page当前页.path URL路径.query URL额外参数.fragment URL锚点.type分页l类型 public ...