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:
- Static allocation, global variables, distribution function and the like, they are before the page is not closed, it will not be cleared
- Dynamic allocation, use
new
to 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:
- In proper use variable scope, as far as possible to declare variables, local variables in the function declaration possible action to avoid global variables
- Be sure to remove the event listener is no longer needed, for example, is about to be removed bound DOM object events
- Avoid caching a lot of data will not be reused
- 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 bar
an object reference, when ob
the end of the call, bar
the object will not be recovered until the variable a
allocation of additional references (or a
goes out of scope).
(3) DOM event, even if the DOM element is removed, the event will not be reclaimed its binding, unless removeEventListener
explicitly remove event
(4) timer, unless explicitly clear the timer