34 Chrome themes scattered garbage collection mechanism

Manual recovery?

ECMAScript is not exposed to any rubbish payback interface, so developers no way to manually force the garbage collection, there is no way interfere memory management.

In as many as the number of cases, developers do not need to manually lift the object reference, simply put on their local variables should be (local variables), garbage can be recycled correctly

V8 memory allocation

When declaring variables and assignment, V8 will be assigned to the variable part of the heap memory. If the application is not enough memory to store variable, V8 will continue to apply until the heap memory size up to the upper limit of the V8.

Heap memory allocation is divided into two ways:

  1. Static allocation, global variables, distribution function and the like, they are before the page is not closed, it will not be cleared
  2. Dynamic allocation, use newto create out, volunteered to allocate space

Rule of thumb

To make Chrome the garbage collector does not retain the object is no longer needed, there are a few things to keep in mind:

  1. In proper use variable scope, as far as possible to declare variables, local variables in the function declaration possible action to avoid global variables
  2. Be sure to remove the event listener is no longer needed, for example, is about to be removed bound DOM object events
  3. Avoid caching a lot of data will not be reused
  4. Less closure

Permanent memory writing

(1) global variables, global variables permanent memory, will be cleared unless refresh the page, leaving the page

(2) object references

function ob(){
  var bar  = new largeObject(); //很大一个对象\变量\字符串
  bar.someCall();
  return bar;
}

var a = new ob();

Now there is a pointer to baran object reference, when obthe end of the call, barthe object will not be recovered until the variable aallocation of additional references (or agoes out of scope).

(3) DOM event, even if the DOM element is removed, the event will not be reclaimed its binding, unless removeEventListenerexplicitly remove event

(4) timer, unless explicitly clear the timer

reference

Guess you like

Origin blog.csdn.net/duola8789/article/details/94553375