How do I create a memory leak in JavaScript?

前端 未结 6 1747
南笙
南笙 2021-01-31 03:34

I would like to understand what kind of code causes memory leaks in JavaScript and created the script below. However, when I run the script in Safari 6.0.4 on OS X the memory co

6条回答
  •  广开言路
    2021-01-31 04:11

    If all you want is to create a memory leak, then the easiest way IMO is to instantiate a TypedArray since they hog up a fixed size of memory and outlive any references. For example, creating a Float64Array with 2^27 elements consumes 1GiB (1 Gibibyte) of memory since it needs 8 bytes per element.

    Start the console and just write this:

    new Float64Array(Math.po2(2, 27))
    

提交回复
热议问题