At our MVC project we have this specific View (HTML/JS) where the user can manipulate a large table: select a line and make some operations including move up/down. With move up/down we are facing memory leak: page starts with ~100 MB and goes up until browser crashes (~8 GB at localhost).
Table data is generated at backend, being sent via JSON to frontend. Each line is treated via JS as below:
function LoadTableClient(selectedLine) {
$('#table-client tbody').find('tr').remove(); //Added for clarification
var listClientInformationJson= $('#generalData').data('listclientinformation');
var html = '';
$.each(JSON.parse(listClientInformationJson), function (index, item) {
html += WriteTableClient(item, index, selectedLine);
});
$('#table-client').find('tbody').append(html);
}
When user move up/down it can affect several rows since each line can also be a group containing several rows inside (and other subgroups and so on). Also, we have subtotal and total columns, therefore we need to redraw the hole table.
For this move up/down operation we are triggering a JS function that requests backend via AJAX, and then it gather the response to redraw our table.
$.ajax({
url: '...../MoveTableClientItem',
type: "POST",
contentType: "application/json",
data: JSON.stringify({
...
}),
success: function (response) {
if (response.success) {
GetProjectInfo(); //gets JSON
LoadTableClient(var1); //read JSON and append to table
}
Update: GetProjectInfo function
function GetProjectInfo() {
$.ajax({
url: '..../GetProjectInfo',
type: "POST",
contentType: "application/json",
async: false,
data: JSON.stringify({
...
}),
success: function (response) {
$('#generalData').data('listclientinformation', '');
$('#generalData').data('listclientinformation', response.listClientInformationJson);
},
error: function (response) {
...
}
});
}
It works fine regarding the visual and functional output, the problem is the memory leak. With Chrome "Memory Inspector" we understood that each operation adds ~30 MB to memory due to rows kept in memory.
Update: see attached print showing JS memory increase.
We tried Location.reload()
but it was not a good user experience since the hole page is reloaded and what the user was doing is lost (open modals, screen position, etc.)
We tried $("#table-client").empty();
at "response.success" but it just didn't work (nothing changed).
Any ideas on how to solve this?