Take the 2-minute tour ×

I'm writing an application which will be contacting a lot of web sites regularly (at their owners request). For this I'd like to be able to tune Java's DNS cache. I can't seem to find anywhere:

  1. The default maximum cache size

  2. How to change this

Any ideas?


Edit: I was concerned about the cache getting to large and running out of memory

share|improve this question
It doesn't have a minimum or maximum size, it has entry TTLs. See Networking Properties. –  EJP Aug 24 '12 at 5:12

3 Answers 3

You will need to use a DNS server, installed on some server, with caching functionality.



share|improve this answer
No you won't, Java has a DNS cache built in, see other answers. –  EJP Aug 24 '12 at 5:13

Java just delegates to the OS. What OS are you using?

Update: that's actually not completely true. The Sun JVM maintains its own cache of resolved hostnames. By default, items in the cache live forever, but you can adjust the TTL downward. Of course, this cache does not persist across JVM invocations; in that case my first sentence stands.

share|improve this answer
Moreover the TTL doesn't seem to work. –  Pangea Aug 23 '12 at 20:55
@Pangea - Never used it myself; after posting I remembered that InetAddress did in fact have something internal, so took a look. I don't think that it's what the OP wants, however. –  parsifal Aug 23 '12 at 20:59
It's the InetAddress internal cache I'm looking at. –  Jonathan Aug 24 '12 at 12:54
@Jonathan - it would have been useful if you'd said in your OP that you were worried about caching too many resolved names. I was assuming that you were worried about wasting time on DNS lookups as items aged out of the cache. –  parsifal Aug 24 '12 at 14:16
Fair point, I've added an edit –  Jonathan Aug 24 '12 at 21:14

I've gone through the implementation for the cache in InetAddress and I'm pretty sure there actually isn't a cache limit. I guess the designers never envisaged that a client could access so many hosts that this would cause us to run out of memory.

share|improve this answer

Your Answer


