This piece of code is creating memory leak issues cause of BufferedReader and InputStreamReader which I think might be happening cause of some exceptions. How should I change it?
try{
URL url = new URL(sMyUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
jsonString += str;
}
in.close();
}catch(Exception e){
}
The code isn't pretty but won't be creating a memory leak. I suggest you use a memory profiler to determine where your memory is being used. Otherwise you are just guessing even if you have ten + years experience performance tuning in Java ;)
A better alternative is to use Java 7
If you have Java 6 or older you can use.
It would be safer to close your stream using a
try..finally
block. You might also use aStringBuilder
as it is designed for concatenating strings. You should also avoid catchingException
and doing nothing with it. Also, your code is concatenating lines without any line-breaks. This may well not be what you want, in which caseappend("\n")
when you read each line in.Here's a version with those modifications: