<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Young</title>
    <description>The latest articles on DEV Community by Young (@o_v_o).</description>
    <link>https://dev.to/o_v_o</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1253398%2Fa31d0413-f131-4180-aa53-16eaeba6f56d.png</url>
      <title>DEV Community: Young</title>
      <link>https://dev.to/o_v_o</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/o_v_o"/>
    <language>en</language>
    <item>
      <title>My first blog here, Simply talking about javascript memory relative things.</title>
      <dc:creator>Young</dc:creator>
      <pubDate>Thu, 18 Jan 2024 14:33:21 +0000</pubDate>
      <link>https://dev.to/o_v_o/my-first-blog-here-simply-talking-about-javascript-memory-leak-4gln</link>
      <guid>https://dev.to/o_v_o/my-first-blog-here-simply-talking-about-javascript-memory-leak-4gln</guid>
      <description>&lt;p&gt;&lt;strong&gt;Care about the basic memory logic in javascript help you write stable code later. Cultivate good habits helps a lot for a beginner&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(If this article has any problems, please point out, thanks!!)&lt;/p&gt;

&lt;p&gt;At the begin, Let we talk about javascript gc(garbage collection) ways.&lt;/p&gt;

&lt;p&gt;As we know, basically there are two normal implementation of gc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reference counting

&lt;ul&gt;
&lt;li&gt;The object will be recycled in gc statement when it's reference counting equals to 0.&lt;/li&gt;
&lt;li&gt;Why not use it? Consider about the snippets of code below, both &lt;code&gt;obj1&lt;/code&gt; and &lt;code&gt;obj2&lt;/code&gt; have one reference counting when leave &lt;code&gt;Leak()&lt;/code&gt; body, these two objects' memory will not be released forever. &lt;/li&gt;
&lt;li&gt;Tips rust use reference counting with smart pointer - &lt;code&gt;WeakRef&lt;/code&gt; to solve these. Which can be seen in js as well.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Leak&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// obj1&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// obj2&lt;/span&gt;
    &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;
    &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;Leak&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Mark-Sweep&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every gc time, javascript engine will mark every object reachable, then watch whole heap, mark unreachable object as unreachable and remove them at the end.&lt;/li&gt;
&lt;li&gt;From the brief introduction, you can see a huge process of gc which may lead to slowness.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;V8 garbage collection&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Talk later. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But in some scenarios, objects will not be recycled forever. We need to pay attention to it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Closure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is closure. &lt;strong&gt;A function which is able to access to a function context's variable&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Closure will never be recycled.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Restore reference to removed dom element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always use global env variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;console&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Yeah, do not use &lt;code&gt;console&lt;/code&gt; in production mode, because &lt;code&gt;console&lt;/code&gt; will restrict engine to recycle objects which is logged.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
