[Memory Leak] 2. Last focused input/textarea element cause memory leak in Chrome

Example Code: 

 <body>
  <div>
    <button class="btn" onclick="createInput(false)"> Button1</button>
    <button class="btn" onclick="createInput(true)">Button2</button>
  </div>

  <script>
    function createInput(isFocus) {
      const inp = document.createElement('input');
      document.body.appendChild(inp);
      isFocus && inp.focus();
      setTimeout(() => {
        inp.remove();
      }, 2000);
    }
  </script>
</body>

 

For button 1, the memory performance looks like this:

 

 

For button 2, the memory peformance looks like this:

 

As you can see for Button 2, not all the memory has been cleared by GC.

 

This bug is caused by Chrome browser... and due to it's minor, therefore there is no plan to fix it.

 

Why it's a minor issue?

That's due to not all the focused input element won't be GC collected, it's only last focused input element

 

Can I blur the inpur before remove the input to solve the problem?

Sadly, nope. This bug doesn't care about whether blur or not, it keep last focused input element in memory.

 

How to resolve rich textarea element problem?

As we know rich textarea element consumpts more memory than input box. What if the last focused element is rich textarea? 

The way to solve this problem is by create one extra small input element and focused on it.

 

posted @   Zhentiw  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2022-11-04 [Typescript] Function scope in typescript
2020-11-04 [Javascript] Broadcaster + Operator + Listener pattern -- 15. Create a Sequence of Broadcasters
2020-11-04 [Kotlin Spring boot] Connect template with a backing bean
2020-11-04 [Kotlin Spring boot] Enable automatically recompile for Spring boot project with devtool
2020-11-04 [Tools MarkDown] Create a Diff in Markdown to Show What Has Changed in a Code Snippet
2020-11-04 [Javascript] Broadcaster + Operator + Listener pattern -- 14. Marking Done Based on a Condition
2019-11-04 [NPM + React] Prepare a Custom React Hook to be Published as an npm Package
点击右上角即可分享
微信分享提示