毕业设计(论文)外文资料翻译-毛浩明_第1页
毕业设计(论文)外文资料翻译-毛浩明_第2页
毕业设计(论文)外文资料翻译-毛浩明_第3页
毕业设计(论文)外文资料翻译-毛浩明_第4页
毕业设计(论文)外文资料翻译-毛浩明_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、毕业设计 (论文 外文资料翻译学院(系 :计算机科学与技术学院专 业:网络工程姓 名:毛浩明学 号: 201217030209外文出处: THINKING IN JAVA附 件: 1.外文资料翻译译文; 2. 外文原文。 附件 1:外文资料翻译译文垃圾收集器的工作方式如果你学下过一种因为在堆里分配对象所以开销过大的编程语言,很自然你可能会 假定 Java 在堆里为每一样东西(除了 primitives分配内存资源的机制开销也会很 大。 不过,事实上垃圾收集器能够深刻影响对象的加速创建。一开始听起来有些奇怪 存贮空间的释放会影响存贮空间的分配,但是这的确是一些 JVMs 的工作方式,并 且这意味

2、着 Java 为堆对象分配存贮空间几乎和别的语言里为栈分配存贮空间一样地 快。举个例子,你可以认为 C+的堆就如同一个堆放的工场,在这个工场里,每一个对 象都立桩为界。对象切实拥有的地皮占有权不久会被废除无效,并且这块地皮必须重新 加以利用。在 Java 的 JVM 里,堆的工作方式完全不同;每次为一个新的对象分配存贮 空间的时候,它就更像是一个不断向前移动的传送带。这就意味着对象存贮空间的分配 速度明显加快。 在这个过程中, “堆指针”简单地向还没被占用的空间领域移动,所以 非常像 C+里栈的分配方式。 (当然,记录工作会有一点额外的开销,但是完全不同于 C+里那种在堆放工场里为寻找没被利用

3、的存贮空间而付出的开销。 你或许观察到实际上堆本身并不是一个传送带,如果你真的那样看待堆,你就会启 用虚拟内存在硬盘里不断地装卸, 结果是看上去你会拥有比实际情况还要多的内存 空间。最终当你创建了足够多的对象后,你会耗尽内存。 Java 的诀窍就在于垃圾搜集 器插手于其中,当垃圾收集器收集垃圾的时候,它会压缩所有堆里的对象以便你能够有 效的将堆指针移动到离传送带更近的地方从而远离了页面错误。 垃圾收集器重新安排了 整个过程,这使得分配存贮空间的时候一种高速,无穷闲置的堆模式成为可能。要想理解 Java 的垃圾收集工作, 先了解一下别的语言系统里垃圾收集所使用的方 案是有帮助的。 一种简单的但却

4、较慢的垃圾收集技术就是引用记数 (refrence counting. 这种技术意味着每个对象都含有一个引用计数器,每一次一个引用指向那个对象的时 候,引用记数就增加一每一次对象引用离开作用域或者被设置为 null的时候,引用记 数就减一。因此,应付对象被引用的数量在你的程序的整个生命周期里是一笔较小但却 一直持续的开销。圾收集器历遍整组对象,当它发现一个引用记数为零的对象时就会释 放那个对象的存贮空间。 (不过,只要记数为零,引用记数方案通常会立刻释放对象 。这种方案的一个缺点是如果对象之间循环着互相引用, 那么这些对象的引用记数可能为 非零,而垃圾收集器依然把它们当作垃圾收集。定位这种自我

5、引用的对象组需要垃圾收 集器付出大量额外的工作。引用记数通常被用来解释一类垃圾收集的工作原理,但是它 似乎没被任何一种 JVM 所采纳。有一种执行更快的垃圾收集方案, 这种方案中垃圾收集不是建立在引用记数的基础 上。相反,它的思想是是任何没死的对象最终一定会在栈和静态存贮器里找到相应存活 的引用。这种链式的查找方式可能历遍几个层次的对象组。因此 , 如果从栈和静态存贮 器里开始并历遍整个引用组,你会找到所有存活的对象。对于你找到的每个单引用,你 必须找到它所指向的对象,然后发觉那个对象的所有引用,接着找到那些引用所指向的 所有对象,依次类推,直到你历遍整个由栈和静态存贮器里的引用所形成的网。每

6、个你 找到的对象必须还存活着。注意,这里不存在分离的自我引用的对象组他们只是没 被查找到,因此被自动当作垃圾。在上述提到的垃圾收集方案中, JVM 使用了一种自适应的垃圾收集方案,它对查找 到 活 对 象 采 取 的 措 施 依 赖 于 它 正 在 使 用 的 方 案 变 体 。 其 中 的 一 个 变 体 就 是 stop-and-copy 。它意味着基于一些明显的原因程序首先停止运行(这不是一 种在后台实施的垃圾收集方案 。然后,每一个活着的对象从一个堆里被拷贝到另一个 堆里,同时被拷贝的活对象和死的对象被当作垃圾遗弃。并且,当对象被拷贝到新的堆 里后,他们在那里被一个挨一个塞紧,因此实现

7、了压缩新堆的目的(而且如前所述,这 种方式腾出了压缩后多余出来的新的空间 。当然,对象从一个地方移动到另一个地方的时候,所有指向对象的引用必须相应改 变。 指向堆或者静态存贮器里某个被移动对象的引用可以立即得到改变,但是还存在 其它后来“在走走”的时候才会碰到的指向该对象的引用。这些引用一旦发现就会被修 改。 (你可以想象存在一张映射旧新地址的表 。有两个问题使这种所谓的 “拷贝型收集器”缺乏效率。第一个问题就是你使用了 两个堆,为了维护两倍于你实际所需要的内存空间,你得在这两个堆之间来回搅动着整 个内存空间。一些 JVM 通过依据实际所需来为堆分配大块内存,然后很简单地从一个块 拷贝对象到另

8、一个块。第二个问题是拷贝过程本身。一旦你地程序趋向于稳定的时候,它可能生成很少或者几乎不生成垃圾。然而 stop-and-copy 方案不管这些,拷贝型垃圾收集器依旧把活 对象占用的空间从一个地方拷贝到另一个地方,这就形成了浪费。为了阻止这种情况的 发生, 一些 JVM会探测没有新垃圾产生的时机, 并且会转向实施另外一个完全不同的垃 圾收集方案。 这种不同的方案被称为 mark-and-sweep,并且它是 Sun 的早期 JVM 版 本一直使用的方案。 处理一般的垃圾收集工作, mark-and-sweep 表现得相当地慢, 但是 当你的程序生成很少或者不生成垃圾时,它又运行得很快。Mark

9、-and-sweep 遵循着和 stop-and-copy 一样的逻辑:从栈和静态存贮器里出发, 跟踪所有的引用从而找到存活的对象。不过,每次它找到活对象的时候,那个对象被做 以标记,而且对象还不会被收集起来。只有在整个标记过程完成后,清扫( sweep工 作才真正开始。在清扫过程中,死对象被释放存贮空间。不过, Mark-and-sweep 方案 的实施过程并没有拷贝压缩的步骤发生, 所以如果垃圾收集器打算压缩已经成为碎片的 堆,它会采用如同洗牌一样的方式来重新安排对象的散乱分布。stop-and-copy 的思想是垃圾收始工作。在 Sun 的文献资料里,你会发现很多资 料认为垃圾收集是一种

10、低优先性的后台进程, 但事实上垃圾收集在早期的 Sun JVM 版本 里并不是这样执行地。相反,当内存闲置空间少的时候, Sun 的垃圾收集器会终止程序 运行。 Mark-and-sweep也需要程序被终止。正如前面提到的,在这里描述的 JVM 里, 内存被分配成大的块。如果你指定了一个大的对象,它将会得到它自己的内存块。严格 意义上的 stop-and-copy 在可以释放旧堆之前,需要从源堆里拷贝每一个活着的对象 到新的堆里,这会耗费大量内存。而有了块的概念,垃圾收集器在收集的时候就能够拷 贝对象到死的块里。每一个块都有一个生成数用来跟踪它是否还活着。正常情况下,只 有自上次垃圾收集后创建

11、的块才被压缩;所有别的块如果在什么地方被引用着的话,相 应的生成记数会增加。这种方式解决了通常情况下许多短期生存的暂时对象。彻底的清 扫工作会周期性进行。大对象仍旧不拷贝(他们只是把自己的生成记数增加 ,而 那些包含小对象的块会被拷贝和压缩。 JVM 会监视垃圾收集的效率,如果是因为所有的 对象都稳定运行而使得收集工作比较浪费时间的 话, 垃圾收集会转向 mark-and-sweep 模 式。 同样地 , JVM 也会跟踪 mark-and-sweep 的运行效果, 如果堆开始变得零碎不堪, 垃圾收集又会转回 stop-and-copy 模式。这就是“自适应”概念的来源,所以你能总结 出一句冗

12、长拗口的话:“自适应地分阶段地 stop-and-copy mark-and-sweep 。 ” JVM 里,可能会有一些附带的加速技术。一项很重要的技术就涉及到那个加载程序的操作。它被 称为 just-in-time( JIT编译器。 JIT 编译器能部分或者全部把程序转换成机器能 够识别的代码,所以程序就不需要 JVM 的解释了,结果是程序运行起来快很多。当必须 加载类的时候(特别是你第一次想创建那个类的对象的时候 ,首先定位 .class文件, 然后相应的字节码被送入内存。 这个时候, 一个可以利用的方式就是直接让 JIT 编译所 有的代码,但是这样做有两个缺点:这种方式的应用由于混杂在

13、程序的整个生命周期 里,所以能累加起来,从而导致程序执行起来会花费一些额外的时间;另外它也增加了 可执行程序的尺寸(字节码要比已扩展的 JIT 代码压缩度高出很多 ,因此有可能导致 虚拟内存的使用,结果明显地降低了程序的运行效率。一个替代方案就是 lazy evaluation ,它的意思是直到代码需要的时候,才用 JIT 来编译。因此,从来都不执行 的代码可能永远都不被 JIT 编译。 在最近的 JDKV 里采用的 Java HotSpot 技术也使用 了一种类似方案, 一段代码每执行一次, 该技术就优化一次代码, 所以运行的次数越多, 代码执行的速度就越快。附件 2:外文原文(复印件How

14、 a garbage collector worksIf you come from a programming language where allocating objects on the heap is expensive,you may naturally assume that Java s scheme of allocating everything (except primitives on the heap is also expensive. However, it turns out that the garbage collector can have a signi

15、ficant impact on increasing the speed of object creation. This might sound a bit odd at first that storage release affects storage allocationbut its the way some JVMs work, and it means that allocating storage for heap objects in Java can be nearly as fast as creating storage on the stack in other l

16、anguages.For example, you can think of the C+ heap as a yard where each object stakes out its own piece of turf. This real estate can become abandoned sometime later and must be reused. In some JVMs, the Java heap is quite different; its more like a conveyor belt that moves forward every time you al

17、locate a new object. This means that object storage allocation is remarkably rapid. The “heap pointer” is simply moved forward into virgin territory, soits effectively the same as C+s stack allocation. (Of course, theres a little extra overhead for bookkeeping, but its nothing like searching for sto

18、rage.Now you might observe that the heap isnt in fact a conveyor belt, and if you treat it that way, youll eventually start paging memory a lot (which is a big performance hit and later run out. The trick is that the garbage collector steps in, and while it collects the garbage it compacts all the o

19、bjects in the heap so that youve effectively moved the “heap pointer” closerto the beginning of the conveyor belt and farther away from a page fault. The garbage collector rearranges things and makes it possible for the high-speed,infinite-free-heap model to be used while allocating storage. To unde

20、rstand how this works, you need to get a little better idea of the way different garbage collector (GC schemes work. A simple but slow garbagecollection technique is is called reference counting. This means that each object contains a reference counter, and every time a reference is attached to an o

21、bject, the reference count is increased. Every time a reference goes out of scope or is set to null, the reference count is decreased. Thus, managing reference counts is a small but constant overhead that happens throughout the lifetime of your program. The garbage collector moves through the entire

22、 list of objects, and when it finds one with a reference count of zero it releases that storage. The one drawback is that if objects circularly refer to each other they can have nonzero reference counts while still being garbage.Locating such self-referential groups requires significant extra work f

23、or the garbage collector. Reference counting is commonly used to explain one kind of garbage collection, but it doesnt seem to be used in any JVM implementations. In faster schemes, garbage collection is not based on reference counting.Instead, it is based on the idea that any nondead object must ul

24、timately be traceable back to a reference that lives either on the stack or in static storage. The chain might go through several layers of objects. Thus, if you start in the stack and the static storage area and walk through all the references, youll find all the live objects. For each reference th

25、at you find,you must trace into the object that it points to and then follow all thereferences in that object, tracing into the objects they point to, etc., untilyouve moved through the entire web that originated with the reference on thestack or in static storage. Each object that you move through

26、must still be alive. Note that there is no problem with detached self-referential groups these are simply not found, and are therefore automatically garbage.In the approach described here, the JVM uses an adaptive garbage-collection scheme, and what it does with the live objects that it locates depe

27、nds on the variant currently being used. One of these variants is stop-and-copy. This means that for reasons that will become apparent the program is first stopped (thisis not a background collection scheme. Then, each live object that is found is copied from one heap to another, leaving behind all

28、the garbage. In addition, as the objects are copied into the newheap, they are packed end-to-end, thus compacting the new heap (and allowingnew storage to simply be reeled off the end as previously described.Of course, when an object is moved from one place to another, all references that point at (

29、i.e., that reference the object must be changed. The reference that goes from the heap or the static storage area to the object can be changed right away, but there can be other references pointing to this object that will be encountered later during the “walk. ” These are fixed up as they are found

30、 (you could imagine a table that maps old addresses to new ones.There are two issues that make these so-called “copy collectors” inefficient. The first is the idea that you have two heaps and you slosh all the memory back and forth between these two separate heaps, maintaining twice as much memory a

31、s you actually need. Some JVMs deal with this by allocating the heap in chunks as needed and simply copying from one chunk to another. The second issue is the copying. Once your program becomes stable, it might be generating little or no garbage. Despite that, a copy collector will still copy all th

32、e memory from one place to another, which is wasteful. To prevent this, some JVMs detect that no new garbage is being generated and switch to a different scheme (this is the “adaptive” part. This other scheme is called mark-and-sweep, and its what earlier versions of Suns JVM used all the time.For g

33、eneral use, mark-and-sweep is fairly slow, but when you know youre generating little or no garbage, its fast.Mark-and-sweep follows the same logic of starting from the stack and static storage and tracing through all the references to find live objects. However,each time it finds a live object, that

34、 object is marked by setting a flag in it, but the object isnt collected yet. Only when the marking process is finished doesthe sweep occur. During the sweep, the dead objects are released.However, no copying happens, so if the collector chooses to compact afragmented heap, it does so by shuffling o

35、bjects around.The “stop -and-copy” refers to the idea that this type of garbage collection is not done in the background; instead, the program is stopped while the garbage collection occurs. In the Sun literature youll find many references to garbage collection as a low-priority background process,

36、but it turns out that the garbage collection was not implemented that way, at least in earlier versions of the Sun JVM. Instead, the Sun garbage collector ran when memory got low. In addition, mark-and-sweep requires that the program be stopped.As previously mentioned, in the JVM described here memo

37、ry is allocated in big blocks. If you allocate a large object, it gets its own block. Strict stop-and-copy requires copying every live object from the source heap to a new heap before you could free the old one, which translates to lots of memory.With blocks, the garbage collection can typically cop

38、y objects to dead blocks as it collects. Each block has a generation count to keep track of whether its alive. In the normal case, only the blocks created since the last garbage collection are compacted; all other blocks get their generation count bumped if they have been referenced from somewhere.

39、This handles the normal case oflots of short-lived temporary objects. Periodically, a full sweep ismade large objects are still not copied (they just get their generation count bumped, and blocks containing small objects are copied and compacted. TheJVM monitors the efficiency of garbage collection and if it becomes a waste of time because all objects are long-lived, then it switches to mark-and-sweep.Similarly, the JVM keeps track of how successful mark-and-sweep is, and if the heap starts to become fragmented, it switches back to stop-and-copy. This is where the “adaptive” part comes in,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论