Im building a application that needs to poll a webserver alot. Right now i create a recursive method that requests the server and when response is returned it starts all over again, i might put in a sleep in between later.
Now because i need this intensive request polling. I would like to optimize the actual code to do as little work for each request as possible.
I figured i could create a HttpGet (request) and DefaultHttpClient onces and just reuse these instances over and over again.
See http://pastebin.com/qa7k7uEY
The problem is im receiving OutOfMemory errors after like 20 requests.
See error log here http://pastebin.com/58Dhh7L4
Is there a better way of handling this many requests??
It’s probably better to post your code in the question itself.
That being said, the problem is that none of your requests can be garbage collected, as none of them ever pass out of scope; you are constantly building the call stack upwards without exiting the
pollmethod.You should use some sort of delaying mechanism that would allow you to exit your method while still scheduling some work to be done. Something like this should suffice: