`
JerryWang_SAP
  • 浏览: 1042193 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

使用Chrome开发者工具分析JavaScript garbage collector(垃圾回收器)

阅读更多

I use the following simple JavaScript code to illustrate:

var JerryTestArray = [];
(function(){
      for( var i = 0; i < 100; i++){
        JerryTestArray[i] = document.createElement("div");
       }
})();

Create a new empty tab in your Chrome, and first create a snapshot with empty heap status by click “Take Snapshot” button:

 

 

The Snapshot1 is generated.

 

 

Now switch to tab Console, paste the JavaScript code and execute it in console.

 

 

And switch to Profiles tab again to make the second snapshot:

 

 

Once done, select the second snapshot, choose “Comparison” and “Snapshot1” as filter:

 

 

We can find out from column “New” that 100 div nodes are created as we expect.

 

 

Since these nodes are not appended to document node so they are invisible to end user, so displayed as “Detached DOM”. The JerryTestArray still holds the reference to each div node so Garbage collector will not touch these nodes.

In order to make Garbage collector recycle the memory occupied by these nodes, just assign another value to JerryTestArray in console:

 

 

Once done, make the third snapshot and compare it with the second. Now we can find that the re-assignment to JerryTestArray will trigger the destruction of those 100 div nodes by Garbage collector:

 

 

Meanwhile, the string we use in assignment could also be inspected via the combination of filters below:

 

 

There is another kind of profile in Chrome development tool which can give you an overview about timeline of memory allocation:

 

 

Click Start button in above screenshot, and paste the following code in console and executed:

var JerryTestArray = [];
(function(){
      for( var i = 0; i < 98; i++){
        JerryTestArray[i] = document.createElement("span");
        JerryTestArray[i].className = "JerryClassName" + i;
       }
})();

After the code is executed, paste the following code and execute:

JerryTestArray[30] = "this is a long test............................end";

Now stop the profile. The profile is displayed as below. The highlighted vertical blue line indicates the timeslot when the 97 Span elements are created. Note that the number of Span elements displayed here is not 98 but 97 since Chrome development tool displays the final status of objects after “stop profile” button is clicked ( the reference to 30th Span element is replaced by String, so it is recycled by GC ).

 

 

You can drag the two vertical bars to define the time range between which you would like to inspect. For example the time range below contains the timeslot when the below assignment occurs:

JerryTestArray[30] = "this is a long test............................end";

 

 

With this gained knowledge now we can check the memory allocation and destruction in some real application. For example click tile “My Tasks” to enter this application, make the first snapshot and click back button within application to return to launchpad, and make the second snapshot and review the comparison result.

 

 

From the result we find out lots of stuff are deleted after we return to launchpad:

 

 

Hover your mouse to a given destructed object and you can review its detail:

 

 

For more tips How I use Chrome development tool in my daily work, please refer to this blog Chrome Development Tool tips used in my daily work

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

0
2
分享到:
评论

相关推荐

    garbage_collector

    系统级编程的课程实验,实现内存的的自动回收管理。 这里需要声明的是,资源不是来自本人,资源来自网络。本着造福广大学生的目的。但是本人实在太菜,不知道如何设置资源分数为0,所以就设置为1. 另外,代码在vs...

    [GA458]IBM Pattern Modeling and Analysis Tool for Java Garbage Collector

    【IBM Pattern Modeling and Analysis Tool for Java Garbage Collector】是一款针对Java虚拟机(JVM)垃圾收集器进行性能分析和建模的专业工具。该工具由IBM开发,主要用于帮助开发者和系统管理员深入理解Java应用...

    IBM Pattern Modeling and Analysis Tool for Java Garbage Collector

    IBM Pattern Modeling and Analysis Tool for Java Garbage Collector 是一个专门针对Java虚拟机(JVM)垃圾收集器进行性能分析和模式识别的工具。该工具旨在帮助开发者和系统管理员深入理解垃圾收集器的工作机制,...

    新一代垃圾回收器ZGC设计与实现.ch01.print1

    Java垃圾回收器是Java虚拟机...了解和选择合适的垃圾回收器对于Java应用的性能至关重要,开发者需要根据应用场景和需求来选择最适合的垃圾回收策略。随着技术的不断进步,未来还将出现更多高效、低延迟的垃圾回收器。

    WAS 垃圾回收器分析工作

    在Java应用程序服务器中,如IBM的WebSphere Application Server (WAS),垃圾回收器(Garbage Collector, GC)是内存管理的关键组件,它负责自动清理不再使用的对象,以防止内存泄漏。当WAS出现死机或者性能问题时,...

    API for Boehm Garbage Collector DLL

    自动垃圾回收虽然会对性能造成一定的影响,但在大对对性能要求不是特别苛刻的场合下,使用自动垃圾回收技术可以极大的方便软件开发,降低由内存泄漏所引发的问题。 C++的自动垃圾回收库早就有了几个,商业和非商业的...

    垃圾回收器

    在IT领域,垃圾回收(Garbage Collection, GC)是编程语言和运行时环境中的一个...开发者需要了解垃圾回收的基本原理,熟悉所使用平台的垃圾回收器特性,并在必要时进行调优,以确保程序在资源管理方面达到最佳状态。

    The Z Garbage Collector 官方介绍文档

    Z Garbage Collector(ZGC)是Oracle开发的一种可扩展的低延迟垃圾收集器,专为满足现代服务器的高性能计算需求而设计。ZGC支持TB级别的堆大小,且其最大垃圾收集(GC)暂停时间被严格控制在10毫秒以下,大大降低了...

    gc.tar.gz_garbage_garbage collection_回收站_垃圾回收

    在IT行业中,垃圾回收(Garbage Collection,简称GC)是一项重要的技术,特别是在使用像Java、Python、Ruby等高级编程语言时。垃圾回收是自动管理内存的一种机制,它负责监测和释放不再使用的内存空间,以防止内存...

    JVM垃圾回收器工作原理及使用实例介绍Java开发Java

    - 使用VisualVM、JProfiler等工具监控和分析垃圾回收情况。 - 通过调整参数优化内存使用和垃圾回收性能。 7. 应用技巧 - 避免创建大量短生命周期对象,减少垃圾回收负担。 - 使用弱引用、软引用等管理内存,让...

    简单c++垃圾回收器

    为了解决这个问题,我们可以设计一个简单的C++垃圾回收器(Garbage Collector)。这个回收器的目的是自动追踪和释放不再使用的堆内存,以防止内存泄漏。 内存泄漏是指程序分配了内存但未能正确地释放它。长期的内存...

    Garbage Collector v1.3.4_GarbageCollector_

    Activation KMS Garbage Collector v1.3.4

    高清完整版 The Garbage Collection Cookbook 垃圾回收算法手册

    The Garbage Collection Cookbook 垃圾回收算法手册

    java垃圾回收器代码举例

    Java垃圾回收器(Garbage Collector, GC)是Java编程语言中的一个重要特性,它负责自动管理内存,自动回收不再使用的对象,以防止内存泄漏。在Java中,程序员无需手动释放内存,这一过程由JVM(Java虚拟机)自动完成...

    细述 Java垃圾回收机制→Types of Java Garbage Collectors 1

    1. Serial Garbage Collector:这是一个单线程的垃圾回收器,主要针对轻量级应用或单处理器环境。它在一个单独的线程中执行垃圾回收,这可能导致应用程序暂停时间较长。可以使用-XX:+UseSerialGC标志来启用它,适合...

    The Garbage Collection Handbook(垃圾回收器手册).7z

    4. **垃圾收集器类型**:Java虚拟机(JVM)提供了多种垃圾收集器,如Serial、Parallel、Parallel Old、CMS(Concurrent Mark Sweep)、G1(Garbage-First)和ZGC(Zing Garbage Collector)。每种收集器有其特定的...

    JVM中[垃圾回收器]的所有内容-pdf

    Java虚拟机(JVM)中的垃圾回收器(Garbage Collector, GC)是Java程序自动管理内存的关键组件。垃圾回收器负责识别不再使用的对象并回收它们所占用的内存,以防止内存泄漏并确保程序的稳定运行。理解不同类型的垃圾...

    细述 Java垃圾回收机制→How Java Garbage Collection Works- - Android 1

    3. Concurrent Mark-and-Sweep (CMS) Garbage Collector:并发标记清除垃圾回收器 4. G1 Garbage Collector:G1垃圾回收器 Java中的引用类型 ---------------- Java中有四种不同的引用类型: 1. Strong Reference...

    lab_garbage_collector

    CMU的垃圾回收实验(IN C) 1.这个实验由我主讲,我制作了PPT,包含三种思路, 2.并提供其中最重要也是最简单的扫描Stack Frame的源码。 3.此外,有整体思考的思维导图,供全局预览。

    C++实现垃圾回收器

    在编程领域,垃圾回收(Garbage Collection, GC)是一种自动管理内存的技术,它负责跟踪和回收不再使用的对象,以防止内存泄漏。C++作为一种静态类型、编译式的语言,其标准库并不内置垃圾回收机制,程序员需要手动...

Global site tag (gtag.js) - Google Analytics