JavaScript garbage collection (i)--memory allocation

Source: Internet
Author: User

Static Assignment (Allocation)

Allocates memory from a static storage area. When the program compiles, the memory is allocated and exists throughout the program's run, such as static variables and global variables.

  As the following image on the Internet: the room as a program, we can put the static allocation of memory as a room of durable furniture. Usually, they do not need to be released and recycled, because no one can throw a wardrobe out of the window every day.

  

Second, automatic distribution (Automatic Allocation)

The method of allocating memory for local variables in the stack, the memory in the stack can be automatically released as the code block exits the stack operation. For example, when executing a function, the storage unit of a local variable within a function can be created on the stack, which is automatically freed when the function ends.

  Here's an online look: This is similar to the people who work in the room, when things are done, they leave themselves, and the space they occupy is automatically released as these people leave.

  

  A stack is a last-in-first-out data structure that can be used to simulate the behavior of stacks in JavaScript.

    var // Create a stack    Arr.push ("Apple"); // pressing element "Apple" ["apple"]    Arr.push ("Orange"); // pressing element "Orange"   ["Apple", "orange"]    Arr.pop (); // pop Up "Orange"      ["Apple"]    Arr.push ("banana"); // Press the element "banana"   ["Apple", "banana"]    Console.log (arr);

  The corresponding memory graph:

  

The results printed in Firefox are as follows:

  

Iii. dynamic Assignment (Allocation)

The way in which memory space is dynamically allocated in the heap to store data. That is, the memory requested by malloc or new when the program is running, we need to release it ourselves with free or delete.

  as below this online look for the figure: The block of memory in the heap is like an empty bottle we've been drinking, and it has to be thrown into the dustbin, or the room will be littered.

  

A heap is a hashing algorithm-based data structure that holds data, and in JavaScript, reference values are stored in the heap.

   functionPerson (id,name,age) { This. ID =ID;  This. Name =name;  This. Age =Age ; }varnum = 10; varBol =true; varstr = "ABC"; varobj =NewObject (); vararr = [' A ', ' B ', ' C ']; varperson =NewPerson ("Pwstrick", 25);

  

Lazy people want to have a home robot to clean up with the side.

In software development, if you are too lazy to release memory, you also need a similar robot-a garbage collector implemented by a particular algorithm. It is the garbage collection mechanism itself that has some flaws that lead to JavaScript memory leaks.

Demo Download:

http://download.csdn.net/detail/loneleaf1/8031073

Resources:

Http://kb.cnblogs.com/page/74836/GC and JS memory leaks

http://kb.cnblogs.com/page/77087/Understanding javascript_01_ Understanding memory allocation

Memory allocation for http://www.cnblogs.com/yuzhongwusan/archive/2012/03/27/2418964.html JavaScript

JavaScript garbage collection (i)--memory allocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.