简体   繁体   English

管理动态加载页面中的javascript内存使用情况

[英]managing javascript memory usage in dynamically loaded pages

At the risk of being down-voted for bring to vague or asking an opinion I am unsure how to handle this situation properly, or if it even matters. 由于不确定性或提出意见而被否决的风险,我不确定如何正确处理这种情况,甚至不确定。

I am developing a single page web application where all pages except the main page (with the menu system) are loaded dynamically via ajax. 我正在开发一个单页Web应用程序,其中除主页(带有菜单系统)外的所有页面均通过Ajax动态加载。 All of the javascript libraries I use (jquery, bootstrap, kendo, and others) are loaded in the main page as well as my application.js and css files. 我使用的所有javascript库(jquery,bootstrap,kendo和其他)以及我的application.js和CSS文件都已加载到主页中。

Each page I load requires it's own javascript and has a corresponding js files named the same as the html file (for my own sanity). 我加载的每个页面都需要它自己的javascript,并具有一个与html文件相同的相应js文件(出于我的理智)。 When the page is downloaded and set in the content element, the page js runs, which is great. 下载页面并将其设置在content元素中时,页面js会运行,这很棒。

I soon realized that the page js is adding any variables it creates to the global javascript space. 我很快意识到页面js会将其创建的所有变量添加到全局javascript空间。 Ideally upon the loading of a different page those variables would go away so javascript garbage collection could eventually reclaim that memory. 理想情况下,在加载其他页面时,这些变量将消失,因此javascript垃圾回收最终可以回收该内存。

To facilitate this I started to re-use the same names for common structures, like kendo viewmodels so when a new page is loaded it replaces any global variables of the same name. 为了简化这一过程,我开始对kendo viewmodels等通用结构使用相同的名称,以便在加载新页面时它将替换相同名称的所有全局变量。

In some cases where there are specialized variables I set their values to null to release the memory in the global ajax loader function. 在某些情况下,如果存在专门的变量,我会将其值设置为null,以释放全局ajax加载器函数中的内存。

I'm considering adding a release() function to all pages that does this and call it before the page is unloaded. 我正在考虑向执行此操作的所有页面添加release()函数,并在页面卸载之前调用它。 Since the release function is global and each page redefines it calling it prior to swapping out the page should work. 由于发布功能是全局的,并且每个页面都重新定义了它,所以在换出页面之前应该对其进行调用。

My questions: 我的问题:

  1. does it matter? 有关系吗? should I care about a few Kb of ram that may be left lying around.. 我是否应该关心可能会留下的几Kb的夯。

  2. Are there better ways of handling this? 有没有更好的方法来解决这个问题?

  3. Am what I'm now doing even effective? 我现在正在做的事情有效吗? Does javascript garbage collection clean up un-referenced ram like I'm thinking it does? javascript垃圾回收会像我想的那样清理未引用的ram吗?

Followup after Giacomo's answer: Giacomo回答后的后续行动

Giacomo your technique works in the sense that the page functions normally and the kendo bindings have access to local viewmodel variable created in the Page routine. Giacomo,您的技术在页面正常运行且kendo绑定可以访问在Page例程中创建的局部viewmodel变量的意义上起作用。 My concern is that I don't know how it is working. 我担心的是,我不知道它是如何工作的。 I omitted the var page = Page(); 我省略了var page = Page(); assignment and simply include Page(); 分配并仅包含Page(); at the end of the script (outside of the Page() function) and there seem to be no detrimental effects. 在脚本的末尾(在Page()函数之外),似乎没有有害影响。

The way I see it is the script is run when the page is set into it's parent element's html (a div), this defines then runs the Page() function, which creates all the local variables and kendo widgets/bindings on the page, then it falls out of scope. 我认为脚本是在将页面设置为其父元素的html(div)中时运行的,它定义然后运行Page()函数,该函数在页面上创建所有局部变量和kendo小部件/绑定,那就超出范围了。 Once the Page() function has completed, it ends and there should be nothing left, however the viewmodel I created does remain because the page and it's binding all work, click events defined in Page() are executed, and so on. 一旦Page()函数完成,它将结束并且应该没有任何内容,但是我创建的viewmodel仍然存在,因为该页面及其绑定了所有工作,执行了在Page()中定义的click事件,依此类推。 So, when will all that stuff go away? 那么,所有这些东西什么时候消失? Will it be de-allocated when I replace the parent's html with a new page? 当我将父母的html替换为新页面时,它会被取消分配吗? I don't see how...of course whatever kendo is doing to keep a reference to the viewmodel seems to keep it alive, I am not confident that it gets released when the page disappears, which is my whole intent. 我不知道如何……当然,kendo为保持对视图模型的引用所做的任何工作似乎都能使其保持活动状态,我不确定页面消失后它是否会被释放,这就是我的全部意图。

It could be that the page = new Page(); 页面可能是new Page(); is essential to the de-allocation process...but I don't know how to determine that. 对于取消分配过程至关重要。但是我不知道如何确定。

Possible Solution? 可能的解决方案?

Can someone comment on the worth of this: 有人可以评论一下这个价值:

//enters here on ajax page load after inserted into DOM
var pageData = {
    someData: 999,
    someArray: ["one","two"],
    functionOne: function(arg) {
        //stuff
    },
    functionTwo: function(arg) {
        //stuff
    }
};

//do some work
pageData.functionOne(pageData.someData);
pageData.functionTwo("abc");

//called before page is removed from DOM
function releasePage() {
    pageData = null;
}

//GC eventually cleans it up

you can put the js code of each page in a closure, like: 您可以将每个页面的js代码放在一个闭包中,例如:

function Page() {
  var var1;
  var var2;

  function myFunc() {
    //...
  }
}

and then assign it to a variable upon the "page switching" 然后在“页面切换”时将其分配给变量

var page = new Page();

you can keep reassigning the same variable every time 您可以每次都重新分配相同的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM