IE和Chrome下的JQuery Datatable内存泄漏

时间:2018-10-17 08:56:48

标签: datatable

我的应用程序正在使用Datatable,它动态地绑定数据和事件。通过Internet Explorer中的Developer工具进行监视时,每台服务器成功响应后,总内存使用量就会增加(增加约29-35 MB)。我尝试使用jquery empty()detach()destroy()null方法来释放DOM的解决方案,但没有一个可以阻止内存增加。甚至在请求前销毁数据表也无济于事。我还尝试删除脚本中的变量,将变量设置为off(),然后将事件绑定到数据表function LoadData(isReset) { var viewId = $jq('#ddlCategory').val(); if (viewId == 'undefined' || viewId == null || viewId == '') { return false; } try { //Clear the variables and remove the datatable ClearFunction(); dtAjaxCall = $jq.ajax({ type: "POST", url: "Ajax/WorkListAjax.aspx/GetData", deferRender: true, contentType: "application/json; charset=utf-8", async: true, dataType: "json", timeout: 0, headers: { Connection: 'keep-alive' }, data: JSON.stringify({ "viewId": viewId }), success: function (response) { //Clear the variables and remove the datatable ClearFunction(); result = response.d; if (result.IsError) { CustomConfirm("dialog-confirm", result.Message, ""); } else if (result.Data != null) { data01 = result.Data; result.Data = null; //set the result.Data as null tableHeaders = ''; //var to store Datatable headers columns = []; //var to store Datatable columns excludeFilters = [];//var to exclude the filters. bulkOperation = data01.BulkOperation; //var to store if bulk operation is required //Create the table header columns dynamically as configured in database $jq.each(data01.Columns, function (i, val) { if (val.HiddenColumn != "Y") { tableHeaders += "<th>" + val.DisplayName + "</th>"; var col = { 'title': val.DisplayName, 'data': val.DataColumnName.toLowerCase() }; columns.push(col); } if (val.FilterColumn >= 0) { excludeFilters.push(val.FilterColumn); } }); data = $jq.parseJSON(data01.Results); //result returned in ajax call json = $jq.parseJSON(data01.WorkListJQStructure); //datatable configuration returned in ajax call delete json["bAutoWidth"]; json.data = data; json.columns = columns; DisplayExportOptions(json.buttons, 'resultsTable', 'ulExportTo'); //Add checkbox for each row in the data table dtColumnDefinition = function (data, type, full, meta) { return '<input type="checkbox" data-id="' + data + '">'; } json.aoColumnDefs[0].render = dtColumnDefinition; //Ajax call to save the datatable state state dtSaveState = function (settings, data) { $jq.ajax({ type: "POST", url: "Ajax/WorkListAjax.aspx/SaveState", contentType: "application/json; charset=utf-8", async: true, dataType: "json", data: JSON.stringify({ "viewId": viewId, json: data }), "success": function () { }, error: function (request, status, error) { CustomConfirm("dialog-confirm", "An error occurred while processing your current request. Please try again", ""); } }); } //Try destroying the existing instance if ($jq.fn.DataTable.isDataTable('#resultsTable')) { $jq('#resultsTable').DataTable().destroy(); } //Make the body empty $jq('#resultsTable tbody').empty(); //Remove the datatable $jq("#resultsTable").dataTable().remove(); //Datatable save state function call json.stateSaveCallback = dtSaveState; //Empty from the parent table of the datatable $jq("#resultsTable_display").empty(); //Create the datatable header dynamically and add to the parent table $jq("#resultsTable_display").append('<table id="resultsTable" class="display" style="width:100%;white-space: nowrap;"><thead><tr>' + tableHeaders + '</tr></thead></table>'); //bind the json and data to the datatable SearchTable = $jq("#resultsTable").DataTable(json).rows().invalidate().draw(); //Set the event off before $jq("#resultsTable").off(); //Set the event $jq('#resultsTable').on('length.dt', function (e, settings, len) { //code to set the height dynamically... }); $jq("#resultsTable_display .dataTables_scrollHeadInner").css("width", "100%"); $jq("#resultsTable_display .dataTables_scrollHeadInner .dataTable").css("width", "100%"); BulkOpr(bulkOperation, SearchTable); //reset the columns after binding the data SearchTable.columns.adjust(); DataTableName = '#resultsTable'; $jq('#resultsTable').on('page.dt', function () { info = SearchTable.page.info(); customHeight = 0; customHeight = UserDefinedFields.CustomPageHeight(info, 40); $jq('#Parent').attr('style', 'min-height:' + customHeight + 'px;'); }); $jq("a").removeClass("dt-button"); } //set the variables null json = null; data01 = null; data = null; }, error: function (request, status, error) { //do nothing... } }); return false; } finally { //Clear the variables... } } //---------------------------------------------- //method to clear the variables and datatables function ClearFunction() { //make all variables null dtAjaxCall = null; resultSearchTable = null; DataTableName = null; info = null; customHeight = null; cells = null; selected = null; cBox = null; clist = null; dtSaveState = null; result = null; data01 = null; tableHeaders = null; columns = null; excludeFilters = null; bulkOperation = null; data = null; json = null; dtColumnDefinition = null; //clear dom objects $jq("#resultsTable").dataTable().remove(); $jq("#resultsTable_display").dataTable().empty(); }

js文件中的代码段如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SportStat.Forme
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void btnUlaz_Click(object sender, EventArgs e)
        {
            ulaz();
        }
        private void ulaz()
        {
            if (txtPassword.Text == "")
            {
                MessageBox.Show("Morate upisati lozinku", "caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);             
                return;
            }
            var sql = "select * from users where password='" + txtPassword.Text + "'";
            var dt = sustav.puniDt(sql);
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Neispravna lozinka", "caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                txtPassword.Focus();
                return;
            }
            if ((string)dt.Rows[0]["password"] != txtPassword.Text)
            {
                MessageBox.Show("Neispravna lozinka", "caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                txtPassword.Focus();
                return;
            }
            sustav i = new sustav();
            i.idUser = (int)dt.Rows[0]["idUser"];
            i.pswd = (int)dt.Rows[0]["pswd"];
            i.prezimeIme = (int)dt.Rows[0]["prezimeIme"];
            i.idKlub = (int)dt.Rows[0]["idKlub"];
            int count = i.pswd;
            if (count == 1)
            {
                MessageBox.Show("Login Successful!");
                this.Hide(); 

                frmMain fm = new frmMain();
                fm.Show();
            } 
            this.Close();
        }
        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

谢谢!

0 个答案:

没有答案