I have a Java application utilizing the MongoDB Java driver (2.11.3). I'm using one static MongoClient instance across the whole application and as stated in the docs it should handle the connection pooling. After a long time (It's a BungeeCord, if you're familiar with Minecraft) it throws Too many open files exceptions. Having a look at

$ lsof -p 7616 -n | grep "123.456.7.8:27017" | wc -l

shows us that there are 16 connections to port 27017. But when we look at:

$ lsof -n | grep "123.456.7.8:27017" | awk '{print $2}' | grep 7616 | wc -l

it shows us a lot more connections than the first command.

First question is why do the 2 commands have different output and the second is whether anyone of you has experienced something similar with the Java MongoDB driver.

closed as unclear what you're asking by Gilles, Michael Homer, jimmij, John WH Smith, peterph Jan 18 '15 at 20:34

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Could you edit your question and explain why you would expect to see the same result? You're grepping for 16062 in the second one. – terdon Jan 17 '15 at 17:04
  • Thanks for the edit, that's clearer. However, without seeing the output of the two commands, we can't tell you what's going on. For example, could you have a PID like 17616? Please include the output of the commands so we can understand. Also, this site is more like a wiki than a forum. Don't add sequential edits, just edit the question itself (I have now done so). – terdon Jan 19 '15 at 16:31

I can't be sure without seeing your actual output but the likeliest explanation is that 27017 appears in different places. Your first command will list files for the process with PID 5253, then prints all lines that contain 27017 anywhere in the line.

Your second command will print all open files and then, again, you are selecting all lines that contain 27017 anywhere in the line. I assume you are actually grepping for 27017 in the second command as well and not for 16062 as you show in your question.

In any case, neither of your commands is looking specifically at port 27017. In fact, I don't even see why you expect that a port will be listed. lsof looks at files, not ports. No ports are shown when I look for ssh in the output of lsof -n on my system for example.

Anyway, to give you a more concrete example:

$ sudo lsof -np 7033 | head
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
firefox 7033 terdon  cwd    DIR                8,6   491520  16646145 /home/terdon
firefox 7033 terdon  rtd    DIR                8,7   548864         2 /
firefox 7033 terdon  txt    REG                8,7   143680   1841618 /opt/firefox/firefox
firefox 7033 terdon  DEL    REG               0,30           10335506 /tmp/.glT5RaDf
firefox 7033 terdon  mem    REG                8,7 12303504   1573445 /usr/share/fonts/truetype/unifont/unifont.ttf
firefox 7033 terdon  DEL    REG                0,4          970489906 /SYSV00000000
firefox 7033 terdon  mem    REG                8,7  7470672    540090 /usr/lib/x86_64-linux-gnu/gstreamer-0.10/libgstffmpeg.so
firefox 7033 terdon  mem    REG                8,7  5177387   1580807 /usr/share/fonts/truetype/wqy/wqy-microhei.ttc
firefox 7033 terdon  mem    REG                8,7   120688    532953 /usr/lib/x86_64-linux-gnu/gstreamer-0.10/libgstvideoscale.so

Your search for 27017 could match in any of the fields. It could be the size, or the PID or the node or whatever. It could also be part of another number. For example:

$ printf "12345\n12" | grep 12

As you can see above, 12 is matched on both lines, not only on the second where it forms a word. You can use the -w switch to make grep match against whole words only:

$ printf "12345\n12" | grep -w 12

So, since you are not restricting the output by PID in the second command, your two greps could be matching anywhere at all on each line so, of course, you have different output.

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