Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Our server recently ran out of file descriptors, and in regards to that I have some questions. ulimit -n is supposed to give me the maximum number of open file descriptors. That number is 1024. I checked the number of open file descriptors by running lsof -u root |wc -l and got 2500 fds. That is a lot more than 1024, so I guessed that would mean the number 1024 is per process, not per user, as I though. Well, I ran lsof -p$PidOfGlassfish|wc -l and got 1300. This is the part I don't get. If ulimit -n is not the maximum number of processes per user or per process, then what is it good for? Does it not apply to the root user? And if so, how could I then get the error messages about running out of file descriptor?

EDIT: The only way I can make sense out of ulimit -n is if it applies the the number of open files (as stated in the bash manual) rather than the number of file handles (different processes can open the same file). If this is the case, then simply listing the number of open files (grepping on '/', thus excluding memory mapped files) is not sufficent:

lsof -u root |grep /|sort  -k9  |wc -l #prints '1738'

To actually see the number of open files, I would need to filter on the name column on only print the unique entries. Thus the following is probably more correct:

lsof -u root |grep /|sort  -k9 -u |wc -l #prints '604'

The command above expects output on the following format from lsof:

java      32008 root  mem       REG                8,2 11942368      72721 /usr/lib64/locale/locale-archive
vmtoolsd   4764 root  mem       REG                8,2    18624     106432 /usr/lib64/open-vm-tools/plugins/vmsvc/libguestInfo.so

This at least gives me number less than 1024 (the number reported by ulimit -n), so this seems like a step in the right direction. "Unfortunately" I am not experiencing any problems with running out of file descriptors, so I will have a hard time validating this.

share|improve this question
2  
lsof reports memory mappings as well as open files, so your 'wc' pipeline yields an over-estimate of the number of file descriptors used by that process. –  Richard Kettlewell Jun 9 '12 at 11:52
    
aha! now that is good info. But I am not quite sure I understand. By "memory mappings", you mean a memory mapped file? That would require a file handle to my understanding, or how else would the OS be able to update the file? –  oligofren Jun 11 '12 at 16:11
    
And followup two: What would be a good way of finding all open file handles - the ones that are actually affected by the limits imposed by "ulimit -n"? –  oligofren Jun 11 '12 at 16:12
1  
Memory mappings don’t require an open file. If you want to list open files only, filtering the output of lsof is probably the easiest approach. –  Richard Kettlewell Jul 7 '12 at 13:49
    
Thanks, edited my answer. Using ´lsof -u root |grep /|sort -k9 -u´ seems to give what amounts to a reasonable answer. This is at least a number less than ulimit -n. –  oligofren Jul 16 '12 at 14:11

4 Answers 4

I tested this in Linux version 2.6.18-164.el5 - Red Hat 4.1.2-46. I could see that the ulimit is applied per process.

The parameter is set at user level, but applied for each process.

Eg: 1024 was the limit. Multiple processes were started and the files open by each one was counted using

ls -l /proc/--$pid--/fd/ | wc -l

There were no errors when the sum of files opened by multiple processes crossed 1024. I also verified the unique file count combining the results for different processes and counting unique files. The errors started appearing only when the count for each process crossed 1024. ( java.net.SocketException: Too many open files in process logs )

share|improve this answer
    
Thanks for testing this out. Do you have any idea why lsof -p$PidOfGlassfish|wc -l gave me 1300? I am guessing the two approaches to counting differ somehow. If not, then maybe the limit does not apply to the root user? –  oligofren May 21 at 7:52

The ulimit is for filehandles. It applies to files, directories, sockets, pipes epolls, eventfds, timerfds etc etc.

At any point during the processes startup the limits might have been changed. Visit /proc/<pid>/limits and see if the values have been altered.

share|improve this answer

It seems like your reasoning is something like, "I have to lower that limit so I don't run out of precious descriptors". The truth is exactly the reverse -- if your server ran out of file descriptors, you need to raise that limit from 1,024 to something larger. For a realistic glassfish implementation, 32,768 is reasonable.

Personally, I always raise the limit to around 8,192 system-wide -- 1,024 is just ridiculous. But you'll want to raise glassfish higher. Check /etc/security/limits.conf. You can add a special entry for the user glassfish runs as.

share|improve this answer
    
I am not sure how you could interpret me to mean that :-) What I was wondering is why it did not seem to apply. I will set it higher, but I want to understand how it works as well. If the limit is 1024, then how could Glassfish have 1300 handles? –  oligofren Jun 11 '12 at 16:08
    
'lsof -u root |grep /|sort -k9 -u' prints the unique file descriptor entries. I guess the number of lines from this is the actual number ulimit -n applies to. –  oligofren Jul 16 '12 at 14:15

You want to take a look at the system-wide limits set in /proc/sys/fs/file-max and adjust it there (until next reboot) or set fs.file-max in sysctl.conf to make it permanent. This might be helpful - http://www.randombugs.com/linux/tuning-file-descriptors-limits-on-linux.html

share|improve this answer
1  
That comment about bash isn't accurate. ulimit imposes a per user-id set of limits, for processes initiated via the shell, which is essentially virtually everything thanks to how the process tree is spawned on Unix like operating systems. It's not bash. –  EightBitTony Jun 8 '12 at 12:39
    
Sorry - will edit, but comment about system wide limits still stands, though. –  rnxrx Jun 8 '12 at 15:07
    
It's very unlikely that he's hitting the system wide limits. Possible, but very unlikely. –  David Schwartz Jun 8 '12 at 20:24
    
EightBitTony: ulimit does not set ulimit per user-id set of limits. Its per process when the pam_limits are applied. The ulimit that that is "per user" is the "ulimit -u" "The maximum number of processes available to a single user" –  Wade Mealing May 21 at 4:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.