In my program I need to track list of opened connections to some HTTP server - in order to disconnect them at once if needed.
I faced the following problem. If I connect to HTTP server everything works perfect, but if to HTTPS, then connections are not removed from the list. It leads to memory leak.
Example:
package test;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
try {
ArrayList<URLConnection> al = new ArrayList<URLConnection>();
URL url = new URL("http://www.example.com");
URLConnection conn = url.openConnection();
al.add(conn);
System.out.println("Result of removing = " + al.remove(conn));
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
}
If URL points to "http://www.example.com" then "Result of removing = true".
But if URL points to "https://www.example.com" then "Result of removing = false".
I don't understand such a behavior. I have some assumptions, but not not sure...
Can anybody help?