Is there a file descriptor leak when using sockets on a linux platform?

If I open and close a socket by calling for instance

Socket s = new Socket( ... );
in = s.getInputStream();

Linux states that this socket is still open or at least the file descriptor for the connection is presen. When querying the open files for this process by lsof, there is an entry for the closed connection:

java    9268 user    5u  sock    0,4           93417 can't identify protocol

This entry remains until the program is closed. Is there any other way to finally close the socket? I’m a little worried that my java application may block to many file descriptors. Is this possible? Or does java keep these sockets to re-use them even is ReuseAdress is set?


If those sockets are all in the TIME_WAIT state, this is normal, at least for a little while. Check that with netstat; it is common for sockets to hang around for a few minutes to ensure that straggling data from the socket is successfully thrown away before reusing the port for a new socket.


Using PowerMock or How much do you let your tests affect your design? [closed]

I’ve been a fan of EasyMock for many years now, and thanks to SO I came across references to PowerMock and it’s ability to mock Constructors and static methods, both of which cause problems when retrofitting tests to a legacy codebase.

Obviously one of the huge benefits of unit testing (and TDD) is the way it leads to (forces?) a much cleaner design, and it seems to me that the introduction of PowerMock may detract from that. I would see this mostly manifesting itself as:

  1. Going back to initialising collaborators rather than injecting them
  2. Using statics rather than making the method be owned by a collaborator

In addition to this, something doesn’t quite sit right with me about my code being bytecode manipulated for the test. I can’t really give a concrete reason for this, just that it makes me feel a little uneasy as it’s just for the test and not for production.

At my current gig we’re really pushing for the unit tests as a way for people to improve their coding practices and it feels like introducing PowerMock into the equation may let people skip that step somewhat and so I’m loathe to start using it. Having said that, I can really see where making use of it can cut down on the amount of refactoring that needs to be done to start testing a class.

I guess my question is, what are peoples experiences of using PowerMock (or any other similar library) for these features, would you make use of them and how much overall do you want your tests influencing your design?


I think you’re right to be concerned. Refactoring legacy code to be testable isn’t that hard in most cases once you’ve learned how.

Better to go a bit slower and have a supportive environment for learning than take a short cut and learn bad habits.

(And I just read this and feel like it is relevant.)


Showing the current date using JSTL formatDate tag

I am trying to show the current date in my JSP page using JSTL. below is the code I am using.

<jsp:useBean id="now" class="java.util.Date" scope="request" />
<fmt:formatDate value="${now}" pattern="MM.dd.yyyy" />

But the above code is not producing any results? Am I missing anything here or is there any better approach for this? I am using JSTL 1.1.


Try to print it without formatting, ${now}, you probably have null in there. Do you have this attribute in request scope? Just double check, if yes it might be null. If not, then try remove this scope attribute from the tag, yeah it doesn’t matter but just to make the things clear.


Running a Java Thread in intervals

I have a thread that needs to be executed every 10 seconds. This thread contains several calls (12 – 15) to a database on another server. Additionally, it also accesses around 3 files. Consequently, there will be quite a lot of IO and network overhead.

What is the best strategy to perform the above?

One way would be to use the sleep method along with a while loop, but that would be a bad design.

Will a class similar to Timer be helpful in this case? Also, would it be better to create a couple of more threads (one for IO and one for JDBC), instead of having them run in one thread?


I find that a ScheduledExecutorService is an excellent way to do this. It is arguably slightly more complex than a Timer, but gives more flexibility in exchange (e.g. you could choose to use a single thread or a thread pool; it takes units other than solely milliseconds).

ScheduledExecutorService executor =

Runnable periodicTask = new Runnable() {
    public void run() {
        // Invoke method(s) to do the work

executor.scheduleAtFixedRate(periodicTask, 0, 10, TimeUnit.SECONDS);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..