JavaScript exploration: Memory Management of JScript

Source: Internet
Author: User
We know that this anonymous function calls the returned function (the function with the identifier g), and then assigns the value to the external f

After learning about these non-conforming code parsing bugs, if we use them, we will find the memory is actually faulty. Let's look at an example:

  var f = (function(){    if (true) {      return function g(){};    }    return function g(){};  })();

We know that this anonymous function calls the returned function (a function with the identifier g) and then assigns the value to the external f. We also know that naming a function expression will lead to the generation of redundant function objects, and this object is not the same as the returned function object. Therefore, this excess g function will die in the closure of the return function, so the memory problem will occur. This is because the function and g in the if statement are declared in the same scope. In this case, unless we explicitly disconnect the reference to the g function, it keeps occupying the memory.

Var f = (function () {var f, g; if (true) {f = function g () {};} else {f = function g (){};} // After g is set to null, it will no longer occupy the memory. g = null; return f ;})();

By setting g to null, the garbage collector recycles the implicit function referenced by g. to verify our code, we will perform some tests, to ensure that our memory is recycled.

Test

The test is simple, that is, the namefunction expression creates 10000 functions and saves them in an array. Wait a while and then check the memory occupied by these functions. Then, disconnect the references and repeat the process. The following is the test code:

  function createFn(){    return (function(){      var f;      if (true) {        f = function F(){          return 'standard';        };      }      else if (false) {        f = function F(){          return 'alternative';        };      }      else {        f = function F(){          return 'fallback';        };      }      // var F = null;      return f;    })();  }  var arr = [ ];  for (var i=0; i < 10000; i++) {    arr[i] = createFn();  }

You can see the following results through the Task Manager running in Windows XP SP2:

  IE6:    without `null`:   7.6K -> 20.3K    with `null`:      7.6K -> 18K  IE7:    without `null`:   14K -> 29.7K    with `null`:      14K -> 27K

As expected, it is shown that disconnecting the reference can release the memory, but not a lot of memory is released. Only 10000 function objects can release about 3 MB of memory, which is nothing for some small scripts, however, it is necessary for large programs or devices that run in low memory for a long time.

About Safari 2. JS parsing in x also has some bugs, but the version is relatively low, so we will not introduce it here. If you want to see it, Please carefully check the English documents.

Additional reading

The topic list of this article is as follows:

  1. How should we understand the working principle of the JavaScript engine?
  2. JavaScript exploration: the importance of writing maintainable code
  3. JavaScript exploration: exercise caution when using global variables
  4. JavaScript exploration: var pre-parsing and side effects
  5. JavaScript exploration: for Loop (for Loops)
  6. JavaScript exploration: for-in loop (for-in Loops)
  7. Exploring JavaScript: Prototypes is too powerful
  8. JavaScript: eval () is the devil"
  9. JavaScript exploration: Using parseInt () for Numerical Conversion
  10. Exploring JavaScript: Basic coding specifications
  11. JavaScript exploration: function declaration and function expression
  12. JavaScript exploration: Name function expressions
  13. JavaScript: function name in the debugger
  14. JavaScript: JScript Bug
  15. JavaScript exploration: Memory Management of JScript
  16. Exploring JavaScript: SpiderMonkey's quirks
  17. JavaScript exploration: an alternative solution to naming function expressions
  18. JavaScript exploration: Object
  19. JavaScript exploration: Prototype chain
  20. JavaScript exploration: Constructor
  21. JavaScript probing: executable context Stack
  22. Execution context 1: Variable object and activity object
  23. Execution context 2: Scope chain Scope Chains
  24. Execution context 3: Closure Closures
  25. Execution context 4: This pointer
  26. Exploring JavaScript: Powerful prototype and prototype chain
  27. JavaScript Functions 1: function declaration
  28. JavaScript function 2: function expressions
  29. JavaScript function 3: function expressions in a group
  30. JavaScript function 4: function Constructor
  31. JavaScript variable object 1: VO Declaration
  32. JavaScript variable object 2: VO in different execution contexts
  33. JavaScript variable object 3: two stages of execution Context
  34. JavaScript variable object IV: Variables
  35. Property of the JavaScript variable object __parent _
  36. JavaScript scope chain 1: Scope chain Definition
  37. JavaScript scope chain 2: function Lifecycle
  38. JavaScript scope chain 3: Scope chain features
  39. JavaScript closure 1: Introduction to closures
  40. JavaScript closure 2: Implementation of closure
  41. JavaScript closure 3: Closure usage

This article is available at http://www.nowamagic.net/librarys/veda/detail/1637.

Related Article

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.