【问题标题】:jquery, kendo memory leaks questionsjquery, kendo 内存泄漏问题
【发布时间】:2016-09-28 07:59:05
【问题描述】:

我有一些关于内存泄漏的问题。 请看一下这段代码:

(function ($) {

$.fn.bnftWindow = function (options) {

    // Default Settings
    var settings = $.extend({
    }, options);

    // Validate parameters
    if (settings.id === undefined || settings.id === null || settings.id === "") {
        throw "bnftWindow id option is undefined, null or empty string";
    }


    // If window container div already exists
    if ($("#" + settings.id).length) {
        alert("bnftWindow id already exists - will be destroyed and recreated");
        // destroy window container div
        $("#" + settings.id).remove();
    }

    // create window container div
    $("body").append("<div id='" + settings.id + "'></div>");

    var currentInstance = $("#" + settings.id);

    return currentInstance.each(function () {

        currentInstance.data("bnftWindow", currentInstance);

        var widgetId = currentInstance[0].id; //The id of the html element used to create the bnft widget

        // Apply settings
        $('#' + widgetId).kendoWindow(
            settings
        );

        var dialog = $('#' + widgetId).data("kendoWindow");

        currentInstance.destroy = function () {


            kendo.destroy($("#" + widgetId));

            $("#" + settings.id).remove();
        }
    });



    function onRefresh(e) {

        // Always center window first
        if (settings.refresh !== undefined) {
            settings.refresh(e);
        }

        var dialog = $("#" + settings.id).data("kendoWindow");
        dialog.center();
    }
};

}(jQuery));

还有这段代码:

function Test()
{
   var element = $("#myElement");
   element.hide();
   var that = this;

   this.ids = { 
      grid: "messagesGrid",
      gridButton: "gridButton",
      composeWnd: "composeWnd"
   };

   this.grid = null; // will store the grid later


        $("#" + this.ids.gridButton).on("click", function () {
            try {
                   // Do something....
                   that.init();
                }
            }
            catch (ex) {
                alert("Error: " + ex);
            }
        });

}

Test.prototype.init = function()
{
   // Do something else...
   var that = this;

   setTimeout(function() {
      that.createWidget();
   }, 500);
}

Test.prototype.createWidget = function()
{
   // Do something else...
   $("#grid").kendoGrid({ // Some properties here });

   // store grid 
   this.grid = $("#grid").data("kendoGrid");
   // Blah blah blah
}
  1. 变量 currentInstance、widget id、dialog、element、that、this.grid 还是导致内存泄漏的事件处理程序?

  2. 如果元素变量导致内存泄漏,则为 $("#myElement").hide();解决方案? (除了在隐藏后将元素变量设置为 null)。

提前谢谢你。

【问题讨论】:

  • 您是否尝试过使用 Chrome 开发者工具进行时间线录制?这将帮助您轻松查看是否存在泄漏。您可以查看this video,了解跟踪内存泄漏的工作流示例。

标签: javascript jquery memory-leaks kendo-ui


【解决方案1】:

以下对我来说可以清理对话框实例:

    _create: function () {           
        var that = this; 
        ...    
        that.frmElement = that.wrapper.kendoWindow({
           ...
           close: function (e) {
               this.destroy();
               that.wrapper.remove("#" + that.options.dialogId);
               that.wrapper.empty();
            }
            ...
        }).data("kendoWindow");
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多