{"id":2934,"date":"2008-01-04T02:00:39","date_gmt":"2008-01-04T01:00:39","guid":{"rendered":"http:\/\/jelmer.jteam.nl\/2008\/01\/04\/too-many-open-files\/"},"modified":"2008-01-04T02:00:39","modified_gmt":"2008-01-04T01:00:39","slug":"too-many-open-files","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/too-many-open-files\/","title":{"rendered":"Too many open files"},"content":{"rendered":"<p>Today I was asked to review a piece of code that a coworker of mine suspected was causing \u201cToo many open files\u201c exceptions in a production environment The code looked innocuous enough. He was using ProcessBuilder to create and run an external application. Regardless I booted into Linux and ran some tests. Now on Linux its fairly easy to find out if a program is leaking file handles. Once a program is started you can look up the process in the \/proc file system. The \/proc filesystem is a virtual filesystem that resides in the kernels memory it provides you with an easy way to change the behavior of the linux kernel and allows you to view information about currently running processes.<\/p>\n<p>To find out what filehandles a java process is holding onto you simply navigate to \/proc\/[PROCESSID]\/fd There\u2019s a file there for every open file handle.<\/p>\n<p>To count all open file handles you can execute ls \/proc\/[PROCESSID]\/fd | wc -w<\/p>\n<p>Using these simple tools I quickly found out that every time the suspect code was run. 3 filehandles where created that were not being released<br \/>\nAs it turns out  you need to explicitly close the file handles allocated by Process (eg. Close inputstream, outputstream and errorstream)  Process does not release them after the invoked program terminates!<\/p>\n<p>This seems to be a little known fact about Process. Its not mentioned in the javadoc for the process class and just about  all the authorative resources get this wrong. Eg.<\/p>\n<p><a href=\"http:\/\/www.javaworld.com\/javaworld\/jw-12-2000\/jw-1229-traps.html\"><br \/>\n http:\/\/www.javaworld.com\/javaworld\/jw-12-2000\/jw-1229-traps.html<\/a><br \/>\n<a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-tiger09304.html\"><br \/>\nhttp:\/\/www.ibm.com\/developerworks\/java\/library\/j-tiger09304.html<br \/>\n<\/a><\/p>\n<p>Etc<\/p>\n<p>At least sun accepted a bug report for improving the documentation. However its been open since 2006..<br \/>\n<a href=\"http:\/\/bugs.sun.com\/bugdatabase\/view_bug.do?bug_id=6462165\">http:\/\/bugs.sun.com\/bugdatabase\/view_bug.do?bug_id=6462165<\/a><\/p>\n<p>Now I must admit I still am kind of curious about the application my colleague is developing because i believe the max number of filehandles is 65536 on most modern linux systems \ud83d\ude42<\/p>\n<p>\nUpdate: you can also use lsof -p PID  for this. The added benefit is that for sockets it will also cross reference the from &#8211; &gt; to for socket connections<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I was asked to review a piece of code that a coworker of mine suspected was causing \u201cToo many open files\u201c exceptions in a production environment The code looked innocuous enough. He was using ProcessBuilder to create and run an external application. Regardless I booted into Linux and ran some tests. Now on Linux [&hellip;]<\/p>\n","protected":false},"author":58,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[31],"tags":[],"class_list":["post-2934","post","type-post","status-publish","format-standard","hentry","category-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Too many open files - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/too-many-open-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Too many open files - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"Today I was asked to review a piece of code that a coworker of mine suspected was causing \u201cToo many open files\u201c exceptions in a production environment The code looked innocuous enough. He was using ProcessBuilder to create and run an external application. Regardless I booted into Linux and ran some tests. Now on Linux [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/too-many-open-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2008-01-04T01:00:39+00:00\" \/>\n<meta name=\"author\" content=\"Jelmer Kuperus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jelmer Kuperus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/too-many-open-files\/\",\"url\":\"https:\/\/trifork.nl\/blog\/too-many-open-files\/\",\"name\":\"Too many open files - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"datePublished\":\"2008-01-04T01:00:39+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/too-many-open-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/too-many-open-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/too-many-open-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Too many open files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2\",\"name\":\"Jelmer Kuperus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g\",\"caption\":\"Jelmer Kuperus\"},\"sameAs\":[\"http:\/\/www.dutchworks.nl\"],\"url\":\"https:\/\/trifork.nl\/blog\/author\/jelmer\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Too many open files - Trifork Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trifork.nl\/blog\/too-many-open-files\/","og_locale":"en_US","og_type":"article","og_title":"Too many open files - Trifork Blog","og_description":"Today I was asked to review a piece of code that a coworker of mine suspected was causing \u201cToo many open files\u201c exceptions in a production environment The code looked innocuous enough. He was using ProcessBuilder to create and run an external application. Regardless I booted into Linux and ran some tests. Now on Linux [&hellip;]","og_url":"https:\/\/trifork.nl\/blog\/too-many-open-files\/","og_site_name":"Trifork Blog","article_published_time":"2008-01-04T01:00:39+00:00","author":"Jelmer Kuperus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jelmer Kuperus","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/too-many-open-files\/","url":"https:\/\/trifork.nl\/blog\/too-many-open-files\/","name":"Too many open files - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"datePublished":"2008-01-04T01:00:39+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/too-many-open-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/too-many-open-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/too-many-open-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"Too many open files"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2","name":"Jelmer Kuperus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g","caption":"Jelmer Kuperus"},"sameAs":["http:\/\/www.dutchworks.nl"],"url":"https:\/\/trifork.nl\/blog\/author\/jelmer\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/58"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=2934"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2934\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=2934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=2934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=2934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}