Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #31  
Old 6th May 2010, 04:27 AM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,110
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

All of them.

Your problem appears to be that the parent thread opens all files... then starts all
threads - this will duplicate any open file within each process - and use up all of the
possible file descriptors. If the parent thread is opening them, then any undesired
fid should be closed by the child thread after it starts.
Reply With Quote
  #32  
Old 6th May 2010, 04:31 AM
newbie14 Offline
Registered User
 
Join Date: Feb 2010
Posts: 104
windows_xp_2003ie
Re: java.net.SocketException: Too many open files

Dear Jpollard,
Yes I do agree with you I have one which is keep listening once is accepted then run a thread which in it will write to a text file and insert into database. I have already verified and make sure all the closing it done well. Because now I am confuse as I know ulimit-n is for the particular user or proces limit? I would like to know for my application what will be the limit for file descriptors?
Reply With Quote
  #33  
Old 6th May 2010, 04:43 AM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,110
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

The normal limit is 1024 open files.

This is partially because the select system call can only handle 1024 file descriptors
for event support.

In the normal client/server model, the server child will close file descriptors inherited
from the parent that are not relevent to the child. The parent server, must close
any file descriptors passed to the child thare are not relevent to the parent.

One problem I have seen with threads is that the "child" does not necessarily
terminate when it's processing has completed - instead, it goes into a wait
state for the next event to be serviced. If the child does not explicitly close the
file descriptors, they will remain open while processing the next descriptor. this
causes the thread to accumulate open descriptors until there are no more
available. This means that care must be taken to ensure the cleanup after
the thread has performed its service.

I don't know if the java threads actually terminate when they complete their
processing, or are put into a work/event queue for reuse. Using a work queue allows
for a much faster thread startup than actually terminating a thread and creating
a new one.
Reply With Quote
  #34  
Old 6th May 2010, 04:50 AM
newbie14 Offline
Registered User
 
Join Date: Feb 2010
Posts: 104
windows_xp_2003ie
Re: java.net.SocketException: Too many open files

Dear Jpollard,
So based on your experience do you think by increasing the 1024 to a bigger number will it help out? Another thing I notice is that each time I restart my server then it takes some time before I get a high number of Established connection. It goes like first hours is like 100 then next hour become 200 and so on. Is this normal that big number connection can not come in the first hour? Thank you.
Reply With Quote
  #35  
Old 6th May 2010, 10:37 AM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

I have tested the example code provided. In that example, parent and children share the same log file, handled by log4j. Each single child opens one input file to read the socket input from and one output file to write to. Provided it reads the EOF character from the input stream, it closes the files always. What can happen is that no EOF character is received, thus the files never get closed. The code could be made more robust by, say, waiting for a predetermined amount of time and then closing the files, terminating the connection. It's easy to test it by running the application provided:
Code:
java -cp path_to_log4j/log4j-1.2.16.jar:. ThreadTest &
Use
Code:
jobs -l
to find out the process id of the background process, and observe the messages that are logged with
Code:
tail -f threadtest.log
From other terminals, open several telnet connections to the listening socket with
Code:
telnet -e"#" localhost 9876
Enter # on a new line to make telnet switch to command mode and Ctrl-D to send the EOF character. When a new connection is established, the process number of open files is increased by two and a message is logged; when the EOF character is sent along the line, the number is decreased by two and another message is logged . If the EOF is not received, only the initial message (files open) is logged and the number of open files keeps growing until the limit is reached. To test, set the limit to a low value, say 35 or 40 (the limit can be reduced by any user but can only be increased by root), with
Code:
ulimit -n 40
before starting the application. Use
Code:
lsof -p n |wc -l
, n being the process number output by `jobs -l', to monitor the number of open files

Last edited by giulix; 6th May 2010 at 10:57 AM.
Reply With Quote
  #36  
Old 6th May 2010, 12:46 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,110
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

Quote:
Originally Posted by newbie14 View Post
Dear Jpollard,
So based on your experience do you think by increasing the 1024 to a bigger number will it help out? Another thing I notice is that each time I restart my server then it takes some time before I get a high number of Established connection. It goes like first hours is like 100 then next hour become 200 and so on. Is this normal that big number connection can not come in the first hour? Thank you.
It won't help - the problem depends on how frequently connections are
made, and that is outside the control of the server.

It it increases at a rate of 100 per hour, then that would be 2400 per day,
16800 per week - so to keep it down you would have to restart the
server at a minimum of every day. You still get a failure if the rate increases
to 200 per hour, or gets burst rates of up to 1000.

---------- Post added at 07:46 AM CDT ---------- Previous post was at 07:30 AM CDT ----------

Quote:
I have tested the example code provided. In that example, parent and children share the same log file, handled by log4j. Each single child opens one input file to read the socket input from and one output file to write to. Provided it reads the EOF character from the input stream, it closes the files always. What can happen is that no EOF character is received, thus the files never get closed. The code could be made more robust by, say, waiting for a predetermined amount of time and then closing the files, terminating the connection. It's easy to test it by running the application provided:
Good point - Another thing I noticed is that all of the exception handlers ignore
the file. They do nothing. If the connection is aborted by the client, does the
server recieve an EOF?, or does it get a "connection reset" exception?

By the way.... the phrase:

Code:
(m=r.read()) != 0)
Isn't really looking for an end of file (my java is very rusty, but from C eof is a -1).
This would seem to hang the thread, causing the number of threads to increase.

I'm used to explicitly looking for an end of file, as a 0 value may be a valid part
of the data.
Reply With Quote
  #37  
Old 6th May 2010, 01:00 PM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

Yes, you're right. I was referring to my example attached to post #21 of this thread. I think the OP is presently running a modified version of the code originally posted by him/her... sorry for the confusion
Reply With Quote
  #38  
Old 6th May 2010, 01:16 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,110
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

I like that one much better - though even it seems to ignore some errors. For
instance:
Code:
        } finally {
            try {
                if(log.isDebugEnabled())
                    log.debug("Closing input/output files");
                if(r != null) {
                    r.close();
                    r = null;
                }
                if(fw != null) {
                    fw.flush();
                    fw.close();
                    fw = null;
                }
            } catch(IOException e) {
                log.fatal(e.getMessage(), e);
            }
        }
If the last "try" block here catches an exception based on the "r.close();", does the
file "fw" get closed?
Reply With Quote
  #39  
Old 6th May 2010, 01:22 PM
stevea's Avatar
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,304
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

If you want to see the files opened by the process use 'lsof' and -p or grep the process name or pid. That should tell you the number of open files/sockets and the specific files/sockets.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #40  
Old 6th May 2010, 02:03 PM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

Quote:
Originally Posted by jpollard View Post
If the last "try" block here catches an exception based on the "r.close();", does the
file "fw" get closed?
Good point. There should be 2 try blocks, one for each file. If I remember correctly, the finally is guaranteed to execute all included statements, even if an exception is raised.

---------- Post added at 03:03 PM CDT ---------- Previous post was at 02:39 PM CDT ----------

Confirmed (tested).
Reply With Quote
  #41  
Old 6th May 2010, 05:36 PM
newbie14 Offline
Registered User
 
Join Date: Feb 2010
Posts: 104
windows_xp_2003ie
Re: java.net.SocketException: Too many open files

Dear Jpollard,
Ok like you suggest I wont be incerasing the value as it wont be helping me either rite. About the phrase I have already change it to (m=r.read()) != -1) that is what my current system is running on. Ok I will run this statement too.
if(r != null) {
r.close();
r = null;
}
if(fw != null) {
fw.flush();
fw.close();
fw = null;
}

---------- Post added at 08:30 AM CDT ---------- Previous post was at 08:29 AM CDT ----------

Dear Giulix,
I dont understand when you say "There should be 2 try blocks, one for each file. " Which one do you meant? Thank you.

---------- Post added at 08:36 AM CDT ---------- Previous post was at 08:30 AM CDT ----------

Dear Stevea,
Ok I run like this lsof -w -p 20480 it give me a list non stop. How if I just want to get the total? Another thing what is the command for me to stop it from the list keep going and where I do paging? Thank you.
Reply With Quote
  #42  
Old 6th May 2010, 08:09 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,110
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

The code in question should have try blocks around both files

Something like:

Code:
        } finally {
            try {
                if(log.isDebugEnabled())
                    log.debug("Closing input/output files");
                if(r != null) {
                    r.close();
                    r = null;
                }
              } catch(IOException e) {
                      log.fatal(e.getMessage(), e);
              }
              try { 
                if(fw != null) {
                    fw.flush();
                    fw.close();
                    fw = null;
                }
               } catch(IOException e) {
                log.fatal(e.getMessage(), e);
            }
        }
Reply With Quote
  #43  
Old 6th May 2010, 10:54 PM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

Ah, the beauty of open source and peer review...
Reply With Quote
  #44  
Old 7th May 2010, 02:11 PM
giulix's Avatar
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: java.net.SocketException: Too many open files

Quote:
Originally Posted by newbie14 View Post
For the log file can I have separate log files on daily basis?
I found a working example here (velocityreviews.com). Just change the name of the file to whatever you like...
Code:
<param name="File" value="whateveryoulike.log" />
Name the XML file log4j.xml
Reply With Quote
  #45  
Old 7th May 2010, 03:52 PM
newbie14 Offline
Registered User
 
Join Date: Feb 2010
Posts: 104
windows_xp_2003ie
Re: java.net.SocketException: Too many open files

Dear Giulix & Jpollard,
I would like to share with you all I just found out and I guess that is the problem of mine. The problem is that my device once it is connected it does not break the connection. So now I am going to use setTimeout. Will update you all with my progress ok. Anyway I will still do the fundamental testing as suggested earlier.
Reply With Quote
Reply

Tags
files, javanetsocketexception, open

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I un-install the open source Java in Fedora 9 kumudini Using Fedora 3 9th September 2008 04:59 AM
F7, Open Office, Java and Nautilus nicstevens42 Using Fedora 5 12th August 2007 06:04 PM
files want to open with java.... StevenMedley Using Fedora 1 4th July 2007 02:05 AM
How will an Open Source Java effect linux? KClaisse Linux Chat 9 28th October 2006 05:51 AM
good news- open source of JAVA and ATI drivers steve941 Using Fedora 2 16th August 2006 12:59 PM


Current GMT-time: 06:56 (Sunday, 26-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat