Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

精读《JS 中的内存管理》 #40

Closed
linhuiw opened this issue Oct 23, 2017 · 2 comments
Closed

精读《JS 中的内存管理》 #40

linhuiw opened this issue Oct 23, 2017 · 2 comments

Comments

@linhuiw
Copy link
Contributor

linhuiw commented Oct 23, 2017

文章地址:https://blog.sessionstack.com/how-javascript-works-memory-management-how-to-handle-4-common-memory-leaks-3f28b94cfbec

学习一下处理常见的内存泄露情况, 在写代码的时候稍微注意一下, 防范内存泄露问题.
PS: sessionstack出品的几篇 How JavaScript works 系列文章都挺不错的.

@ascoders
Copy link
Owner

C++ 作者说过:C 很容易让你朝自己的脚开枪,尽管在 C++ 中,这么做困难了,但只要不注意,就可能崩掉整条腿。

JS 这类高级语言,隐藏了内存管理功能。但无论开发人员是否注意,内存管理都在那,所有编程语言最终要与操作系统打交道,在内存大小固定的硬件上工作。不幸的是,即使不考虑垃圾回收对性能的影响,2017 年最新的垃圾回收算法,也无法智能回收所有极端的情况。

唯有程序员自己才知道何时进行垃圾回收,而 JS 由于没有暴露显示内存管理接口,导致触发垃圾回收的代码看起来像“垃圾”,或者优化垃圾回收的代码段看起来不优雅、甚至不可读。

所以在 JS 这类高级语言中,有必要掌握基础内存分配原理,在对内存敏感的场景,比如 nodejs 代码做严格检查与优化。谨慎使用 dom 操作、主动删除没有业务意义的变量、避免提前优化、过度优化,在保证代码可读性的前提下,利用性能监控工具,通过调用栈定位问题代码。

@linhuiw linhuiw closed this as completed Oct 28, 2017
@ascoders ascoders mentioned this issue Oct 29, 2017
65 tasks
@liSong5713
Copy link

这个代码通过目前的chrome 72来跑没有发生内存泄漏,是在低版本中有问题吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants