In recent days, I've seen some articles about JavaScript memory management, which is simpler than the Java JVM's memory management.
In the process of learning, found that many netizens talked about circular references, saying that circular references will cause memory leaks, garbage collector can not be recycled.
In fact, not so scary, according to the current understanding of the side dish, this circular reference caused by the memory leak, will only happen in the low version of IE browser, modern browser is not so stupid.
For example, there are two types of popular sayings on the Internet:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <Metaname= "Viewport"content= "width=640, initial-scale=0.5, User-scalable=no" />6 <title>Circular Reference Memory Analysis</title>7 <style>8 </style>9 </Head>Ten <Body> One <inputtype= "button"onclick= "problem ();"value= "Call problem"> A <inputtype= "button"onclick= "mybindevent ();"value= "Call mybindevent"> - </Body> - <Script> the //implicit circular references due to closures - functionmybindevent () { - varobj=Document.createelement ("Div"); - Obj.onclick=function(){ + //even if it ' s a empty function - }; + } A at //Explicit Circular Reference - functionproblem () { - varObja= NewObject (); - varOBJB= NewObject (); - - Obja.someotherobject=OBJB; in Objb.anotherobject=Obja; - } to </Script> + - </HTML>
A simple page with two buttons on top, each calling two ways of causing a memory leak.
With the help of Chrome's profiles feature, a snapshot of the memory was generated, and then compared, the two were found to be free of leaks under Google's browser.
The specific approach is:
- Open the page without any action to directly generate a page memory snapshot.
- Click the button, and then generate the memory snapshot again.
- Compare two times memory changes.
Repeat this process, generate 7, 8 snapshots, tend to stabilize, you will find that the memory has not changed at all.
Each time a snapshot is generated, a GC (garbage collection) is enforced, indicating that each of our constructed circular references is immediately recycled, so it does not appear in the snapshot.
Then, from a theoretical point of view, why should it be recycled?
Because of these circular references, it is not a valid reference. It can be simply understood that only references originating from the stack are valid . The reference in this example is a cross reference to the heap object, although the reference count is not 0, but unreachable , and is eliminated directly when the memory is reclaimed.
In further, the low version of IE browser uses a reference counting mechanism to reclaim memory, mutual references cause each other count is not 0, resulting in a non-recyclable.
The modern browser, the use of the Cheney algorithm, roughly the memory is divided into two parts, and constantly copied back and forth, so that those unreachable objects, it can not be copied, nature is recycled.
Stacks, static, constant, and so on, usually represents the root area, only the references from these places, is reachable, effective.
Queries about JavaScript memory leaks