rahul
rahul

Reputation: 215

how to clean cache JAVA application

i have made an application using java.net library to capture the time taken by a webpage to open..example code

 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 for(int i=0; i<20; i++ )
  {
   conn.disconnect();
   conn.connect();
   long starTime = System.currentTimeMillis();
   JEditorPane editorPane = new JEditorPane();
   editorPane.setPage(new URL());
   long elasedTime = System.currentTimeMillis() - starTime;
   System.out.println(elasedTime);
  }

but i am facing a problem, the cache!! if i open webpages repeatedly then in some cases it shows time=0 mills...that is certainly not possible, please anybody help me on this!!

Upvotes: 2

Views: 2488

Answers (4)

Ravi Jain
Ravi Jain

Reputation: 1482

rahul , there is a huge mistake in your program. Your program is not giving expected results because you have not written it properly , specifically speaking your program is not measuring the time taken by a webpage to open at all.

try this code-->

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 for(int i=0; i<20; i++ )
  {
   long starTime = System.currentTimeMillis();       
   conn.connect();

   JEditorPane editorPane = new JEditorPane();
   editorPane.setPage(new URL());

   long elasedTime = System.currentTimeMillis() - starTime;
   System.out.println(elasedTime);

   conn.disconnect();
  }

Strange , no one could notice mistake in your program.

Upvotes: 1

hmjd
hmjd

Reputation: 121981

setUseCaches(), inherited from URLConnection, should provide what is required:

public void setUseCaches(boolean usecaches)

Sets the value of the useCaches field of this URLConnection to
the specified value. 

Some protocols do caching of documents. Occasionally, it is important to be
able to "tunnel through" and ignore the caches (e.g., the "reload" button in
a browser). If the UseCaches flag on a connection is true, the connection is
allowed to use whatever caches it can. If false, caches are to be ignored.
The default value comes from DefaultUseCaches, which defaults to true. 

It must be called before the connect().

Upvotes: 2

UVM
UVM

Reputation: 9914

You can chose not to use any existing cache by setting conn.setUseCaches(false).This might improve the accuracy in calculation.

Upvotes: 1

Nikolay Antipov
Nikolay Antipov

Reputation: 920

You can disable caching in URLConnection (or HttpURLConnection).

See

Upvotes: 0

Related Questions