自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

React內(nèi)存泄露常見問題解決方案

安全 網(wǎng)站安全
程序的運行需要內(nèi)存。只要程序提出要求,操作系統(tǒng)或者運行時(runtime)就必須供給內(nèi)存。不再用到的內(nèi)存,沒有及時釋放,就叫做內(nèi)存泄漏(memory leak)。

什么是內(nèi)存泄露

  • 程序的運行需要內(nèi)存。只要程序提出要求,操作系統(tǒng)或者運行時(runtime)就必須供給內(nèi)存。
  • 對于持續(xù)運行的服務(wù)進(jìn)程(daemon),必須及時釋放不再用到的內(nèi)存。否則,內(nèi)存占用越來越高,輕則影響系統(tǒng)性能,重則導(dǎo)致進(jìn)程崩潰。
  • 不再用到的內(nèi)存,沒有及時釋放,就叫做內(nèi)存泄漏(memory leak)。

[[261494]]

JavaScript 中常見的幾種內(nèi)存泄露

  • 全局變量引起的內(nèi)存泄漏
    1. function leaks(){  
    2.  leak = '***'; //leak 成為一個全局變量,不會被回收 
  • 閉包引起的內(nèi)存泄漏
    1. var leaks = (function(){  
    2.  var leak = '***';// 被閉包所引用,不會被回收 
    3.  return function(){ 
    4.  console.log(leak); 
    5.  } 
    6. })() 
  • dom清空或刪除時,事件未清除導(dǎo)致的內(nèi)存泄漏
    1. document.querySelector("#demo").addEventListener('click', myFunction); 
    2. var para1=document.querySelector("#demo"); 
    3. para1.parentNode.removeChild(para1); 

如果我們在沒有取消 click 方法前去銷毀了 para1 節(jié)點,就會造成內(nèi)存泄露。

正確的做法:

  1. document.querySelector("#demo").addEventListener('click', myFunction); 
  2. // 我們需要在刪除節(jié)點前清除掛載的 click 方法 
  3. document.querySelector("#demo").removeEventListener("click", myFunction); 
  4. var para1=document.querySelector("p1"); 
  5. para1.parentNode.removeChild(para1); 

具體的示例

1. Demo1:

  1. componentWillMount: function () { 
  2.  var onLogin = this.props.onLogin || function () {}, 
  3.  onLogout = this.props.onLogout || function () {}; 
  4.  this.on('authChange', function () { 
  5.  console.log('user authenticated:', this.state.isAuthenticated); 
  6.  return this.state.isAuthenticated 
  7.  ? onLogin(this.state) 
  8.  : onLogout(this.state); 
  9.  }.bind(this)); 

上面的例子是在 Stack Overflow 上看到的,樓主在componentWillMount的時候掛載了authChange事件,然后 react 出現(xiàn)了如下的報錯:

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method

意思為:我們不能在組件銷毀后設(shè)置state,防止出現(xiàn)內(nèi)存泄漏的情況

(1) 需要怎么解決啦?

添加如下代碼即可:

  1. componentWillUnmount: function () { 
  2. this.off('authChange', this.authChange); 
  3. this.authChange = null

很明顯這種情況就是在 dom 結(jié)構(gòu)銷毀的時候,事件卻沒有清除導(dǎo)致的內(nèi)存泄漏,所以我們需要在componentWillUnmount的時候去清除掛載的方法

(2) react 內(nèi)存泄露相關(guān)解釋和解決方法

這里就提到了內(nèi)存泄露,當(dāng)我們在使用事件綁定,setInterval,setTimeOut 或一些函數(shù)的時候,但是卻沒有在組件銷毀前清除的時候會造成內(nèi)存泄露。這里我們手動的再componentWillUnmount去清除相關(guān)的方法即可。

2. Demo 2

下面這種就是常見的情況:

  1. this.pwdErrorTimer = setTimeout(() => { 
  2.  this.setState({ 
  3.  showPwdError:false 
  4.  }) 
  5. }, 1000); 

設(shè)置了一個timer延遲設(shè)置state,然而在延遲的這段時間,組件已經(jīng)銷毀,則造成此類問題

解決方法:

利用生命周期鉤子函數(shù):componentWillUnmount

  1. componentWillUnmount(){ 
  2.  clearTimeout(this.pwdErrorTimer); 
  3.  clearTimeout(this.userNameErrorTimer); 
責(zé)任編輯:趙寧寧 來源: 今日頭條
相關(guān)推薦

2010-08-26 12:59:29

marginCSS

2010-08-04 10:20:30

Flex組件開發(fā)

2025-02-19 08:00:00

移動端移動設(shè)備移動開發(fā)

2021-08-20 15:49:13

電腦主板維修

2010-09-27 13:14:42

JVM內(nèi)存限制

2009-12-24 11:13:41

2011-01-21 14:13:10

2012-05-09 10:08:41

跨機(jī)房

2010-01-05 10:02:56

LinuxRAID常見問題

2011-07-28 11:28:21

SQL Server數(shù)注冊表編輯器

2019-10-08 16:05:19

Redis數(shù)據(jù)庫系統(tǒng)

2021-05-18 08:21:38

React HooksReact前端

2010-03-30 16:04:34

Linux Nginx

2010-01-13 21:06:37

雙絞線

2011-05-03 17:22:59

激光打印機(jī)

2014-01-07 13:54:02

HadoopYARN

2011-04-27 16:04:12

投影機(jī)

2011-08-22 14:10:51

nagios

2011-09-01 14:04:41

光纖光纖熔接機(jī)

2010-12-31 16:31:08

服務(wù)器常見問題
點贊
收藏

51CTO技術(shù)棧公眾號