Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Input Stream can leak resources #100

Open
kkieffer opened this issue Oct 13, 2020 · 1 comment
Open

Cache Input Stream can leak resources #100

kkieffer opened this issue Oct 13, 2020 · 1 comment

Comments

@kkieffer
Copy link
Contributor

When running offline, panning and zooming causes tiles to try and load however connection.getInputStream() throws an UnknownHostException. If the user keeps trying to load new tiles eventually it throws a SocketException - too many open files (even if the network later comes back online, tiles no longer load and throw this exception, see below). I suspect url.openConnection() needs to be closed (closing the input stream does this automatically).

Some info from the web suggests casting the URLConnection to HttpURLConnection, and checking that a call to its getResponseCode() method == HTTP_OK. Also in any case a finally clause to its disconnect() method.

java.net.SocketException: Too many open files
	at java.base/java.net.Socket.createImpl(Socket.java:546)
	at java.base/java.net.Socket.connect(Socket.java:647)
	at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:290)
	at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
	at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
	at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:177)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1194)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1082)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:163)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1595)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1523)
	at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
	at org.jxmapviewer.viewer.AbstractTileFactory$TileRunner.cacheInputStream(AbstractTileFactory.java:433)
@kkieffer
Copy link
Contributor Author

As it turns out, this is not a bug per se, but occurs because the java.net.Socket is opening a native resource (socket handle) and apparently it does not release the native resource until it is garbage collected. In my case I guess the GC was not running often enough to avoid the issue.

My solution is to keep track of a count of tiles that fail to load. When it reaches some threshold I call System.gc(). I know its not advisable to be calling this but it does solve the problem.

Similar issue posted here:
https://stackoverflow.com/questions/15917383/java-net-unknownhostexception-and-java-net-socketexception-too-many-open-files/64344960#64344960

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant