<?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: Thomas Melville</title>
    <description>The latest articles on DEV Community by Thomas Melville (@thomaswdmelville).</description>
    <link>https://dev.to/thomaswdmelville</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%2F23804%2Fd7f39b5d-c360-44c2-8bd0-c4c2ecb9e5bb.png</url>
      <title>DEV Community: Thomas Melville</title>
      <link>https://dev.to/thomaswdmelville</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thomaswdmelville"/>
    <language>en</language>
    <item>
      <title>How file descriptors can bring your service down. </title>
      <dc:creator>Thomas Melville</dc:creator>
      <pubDate>Thu, 25 Oct 2018 20:12:45 +0000</pubDate>
      <link>https://dev.to/thomaswdmelville/how-file-descriptors-can-bring-your-service-down--17ke</link>
      <guid>https://dev.to/thomaswdmelville/how-file-descriptors-can-bring-your-service-down--17ke</guid>
      <description>&lt;p&gt;Over the past year I've completed the Linux Foundation's Essentials of Linux Administration course. Today I'm going to focus on file descriptors and how your app creates them, and &lt;strong&gt;should&lt;/strong&gt; close them.&lt;/p&gt;

&lt;p&gt;Let's start with a very high level definition of how a linux OS sees its world. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Everything in a running linux system is a file. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A file descriptor is a file which the linux kernel creates and deletes for every file opened and closed by a process. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The linux kernel allows for a finite number of file descriptors!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's see this in action. Start your app and get the PID.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;java &lt;span class="nt"&gt;-jar&lt;/span&gt; app.jar
ps &lt;span class="nt"&gt;-ef&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;app.jar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now cd into processes directory and do an ls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /proc/3535
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-al&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is all the information about your running app that the kernel needs. &lt;/p&gt;

&lt;p&gt;Let's focus on the file descriptors directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-al&lt;/span&gt; /proc/3535/fd
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-al&lt;/span&gt; /proc/3535/fd | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You'll see there are number of open file descriptors.&lt;/p&gt;

&lt;p&gt;Use your app for a bit and come back to this directory and execute the above command again. &lt;/p&gt;

&lt;p&gt;How many descriptors are there now?&lt;/p&gt;

&lt;p&gt;Go back to your app again and use it some more. &lt;/p&gt;

&lt;p&gt;How many are there now?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If this number keeps going up you're heading for trouble!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Eventually you're app won't be able to do anything as it can't open anymore file descriptors. &lt;/p&gt;

&lt;p&gt;From my experience the main culprits are &lt;strong&gt;i/o streams&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;So, how do we solve this?&lt;/p&gt;

&lt;p&gt;Easy!&lt;/p&gt;

&lt;p&gt;Close your streams. &lt;/p&gt;

&lt;h1&gt;
  
  
  Java
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html"&gt;Try-with-resources&lt;/a&gt; to the rescue. Open all you're streams in a try-with-resources block and the jvm will handle closing the steam for you&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;//...&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InputStream&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Python
&lt;/h1&gt;

&lt;p&gt;Python has a similar concept&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ...
&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'mine.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'm sure other languages have the same concept so feel free to comment with it and I'll update the post. &lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;always close your streams so that your app doesn't eventually fall over.&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>java</category>
      <category>linux</category>
      <category>devops</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
