zoukankan      html  css  js  c++  java
  • Java Knowledge series 2

    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.
  • 相关阅读:
    python print 在命令行打印带颜色
    Cython 一篇通
    gcc 内置函数
    gcc 内置函数
    #define xxx do{...} while(0) 宏定义
    防止cpu 一直被占用 sleep(0) 和 yield
    golang之archive/tar包的使用
    go+mysql实现页面的增删改查练习
    设计模式之访问者模式
    设计模式之模板模式(PHP实现)
  • 原文地址:https://www.cnblogs.com/iiiDragon/p/3237779.html
Copyright © 2011-2022 走看看