这个内存泄漏怎么造成的?

时间:2018-04-25 21:49:09

标签: javascript dom memory-management

更新:我想我必须假设我的理解是对的(可能,但如果您有更好的解释,请让我知道),它只需要花多少时间将Chrome转换为GC分离的DOM(我不知道应该花多长时间,很多时候很长一段时间它都没有GC)

enter image description here

所有

我对Javascript内存管理很陌生,当我阅读Google Chrome(版本65.0.3325.181(官方构建)(32位)Windows 7)Devtool教程时,有一个例子:

Discover detached DOM tree memory leaks with Heap Snapshots

add_action( 'woocommerce_before_single_product', 'check_if_variable_first' );
function check_if_variable_first(){
    if ( is_product() ) {
        global $post;
        $product = wc_get_product( $post->ID );
        if ( $product->is_type( 'variable' ) ) {
            // removing the price of variable products
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

// Change location of
add_action( 'woocommerce_single_product_summary', 'custom_wc_template_single_price', 10 );
function custom_wc_template_single_price(){
    global $product;

// Variable product only
if($product->is_type('variable')):

    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice && $product->is_on_sale() ) {
        $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
    }

    ?>
    <style>
        div.woocommerce-variation-price,
        div.woocommerce-variation-availability,
        div.hidden-variable-price {
            height: 0px !important;
            overflow:hidden;
            position:relative;
            line-height: 0px !important;
            font-size: 0% !important;
        }
    </style>
    <script>
    jQuery(document).ready(function($) {
        $('select').blur( function(){
            if( '' != $('input.variation_id').val() ){
                $('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
                console.log($('input.variation_id').val());
            } else {
                $('p.price').html($('div.hidden-variable-price').html());
                if($('p.availability'))
                    $('p.availability').remove();
                console.log('NULL');
            }
        });
    });
    </script>
    <?php

    echo '<p class="price">'.$price.'</p>
    <div class="hidden-variable-price" >'.$price.'</div>';

endif;
}

        }
    }
}

我想原因是我还没有理解DOM在内存中是如何工作的。

我的想法是:每次点击时,为var detachedNodes; function create() { var ul = document.createElement('ul'); for (var i = 0; i < 10; i++) { var li = document.createElement('li'); ul.appendChild(li); } detachedNodes = ul; } document.getElementById('create').addEventListener('click', create); 构建一个执行上下文,在那里输入一个本地var ul引用堆中生成的UL对象,然后在堆中生成10 li,并与UL对象连接,然后由detachedNode引用UL对象。 我想:在create()调用之后,detachedNode和之前的UL对象之间的引用将被破坏(在此单击之前调用create()期间生成),而document.createElement只创建但不附加,所以应该对GC对象进行GC。

我想知道为什么堆中的UL对象不能被GC?

enter image description here

1 个答案:

答案 0 :(得分:3)

如果我单击该按钮一次,它会在该ul元素下创建一个DOM树,然后将其分配给全局detachedNodes变量(即detachedNodes将引用该对象存储器)。

如果我再次点击它,它会为ul分配一个新的detachedNodes元素树,但现在旧的ul元素将不再被引用。在这种情况下,第一次单击的ul树将被垃圾收集,因为不再存在对它的引用。

所以回答你的问题:是的,第一个节点可以被垃圾收集。您链接到的Google文章似乎并不暗示其他情况;根据我的看法,它不会多次点击按钮。

相关问题