菜鸟教程小白 发表于 2022-12-13 03:56:26

ios - React-Native + Redux + ImmutableJS 内存泄漏


                                            <p><p>我的 React-Native 应用程序中有一个奇怪的内存泄漏。这是一个不断增加的 RAM。
我的状态被规范化,然后转换为不可变状态。有一个套接字处理程序可以更新状态中的现有对象。这会导致 RAM 在新消息更新状态时缓慢增加。</p>

<p><strong>状态</strong>:</p>

<pre><code>const state = {
    entities: {
      2000: {
            1: {
                id: 1,
                name: &#34;I am normalized&#34;,
                coordinates:[
                  {
                        lat: 0,
                        lng: 0
                  }
                ]
            },
            2: {
                id: 2,
                name: &#34;me too&#34;,
                coordinates:[
                  {
                        lat: 0,
                        lng: 0
                  }
                ]
            }
      },
      1337: {
            2: {
                id: 2,
                name: &#34;me too&#34;,
                coordinates:[
                  {
                        lat: 0,
                        lng: 0
                  }
                ]
            },
            3: {
                id: 3,
                name: &#34;also normalized&#34;,
                coordinates:[
                  {
                        lat: 0,
                        lng: 0
                  }
                ]
            }
      }
    },
    results: {
      2000: ,
      1337:
    },
};
</code></pre>

<p>然后使用 <code>fromJS()</code> 将其转换为不可变状态。</p>

<p>我有一个套接字处理程序,它将 <code>action.payload</code> 传递给 reducer 。</p>

<pre><code>action = {
    payload: {
      message_type: COORDINATES_UPDATE,
      messages: [
            {
                id: 1,
                coordinates: [
                  {
                        lat: 180,
                        lng: 180
                  }
                ]
            },
            {
                id: 2,
                coordinates: [
                  {
                        lat: 90,
                        lng: 90
                  }
                ]
            }
      ]
    }
}
</code></pre>

<p><strong>处理传入 Action 的reducer</strong>:</p>

<pre><code>case SOCKET_MESSAGE: {
    let newState = state;
    if(action.payload.message_type == &#34;COORDINATES_UPDATE&#34;) {
      action.payload.messages.map((incoming_message) =&gt; {
            let id = incoming_message.id;
            let coordinates = incoming_message.coordinates;
            newState.get(&#34;results&#34;).map((data, entities_id) =&gt; {
                if(data.indexOf(id) &gt; -1) {
                  newState = newState.setIn([&#34;entities&#34;, entities_id, &#34;&#34; + id, &#34;coordinates&#34;], fromJS(coordinates));
                }
            })
      })
      return newState;
    }
}
</code></pre>

<p>这会在 <code>results</code> <code>Map()</code> 中搜索现有的 <code>id</code>,如果确实存在,它会更新实体对象。据我所知,这个逻辑没有问题,状态正确更新并反射(reflect)在 <code>render()</code> 组件中,尽管出于调试目的,我正在渲染一个空的 <code><View/></code> 作为我的整个应用程序,并且只更新状态。</p>

<p>但是,每个 <code>setIn</code> 或 <code>updateIn</code> 都会略微增加 RAM,并且随着更新频率的增加,我得到的内存会在几分钟内增长到 GB。</p>

<p> <a href="/image/zJn0O.jpg" rel="noreferrer noopener nofollow"><img src="/image/zJn0O.jpg" alt="Steady RAM increase"/></a> </p>

<p>相关包:</p>

<pre><code>&#34;react&#34;: &#34;16.0.0&#34;,
&#34;react-native&#34;: &#34;0.50.3&#34;,
&#34;immutable&#34;: &#34;^3.8.2&#34;,
&#34;normalizr&#34;: &#34;^3.2.4&#34;,
&#34;redux&#34;: &#34;^3.7.2&#34;,
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>哦,那是一个巨大的;)
您可能应该检查两件事:</p>

<p>0) 你有多少个套接字连接?您可能有 5-10 个,所有数据都相乘</p>

<p>1) 你使用 redux-dev-tool 吗?在您的情况下,它可能会消耗大量内存,请考虑停用以进行生产/测试</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - React-Native &#43; Redux &#43; ImmutableJS 内存泄漏,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48574113/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48574113/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - React-Native &#43; Redux &#43; ImmutableJS 内存泄漏