octo-online/robospice — pull request #191 by seva-ask (2013-09-23 18:39:05+00:00)
Title: Decrease memory footprint

I have found the cause of OutOfMemory exception on Sony Tablet S.
It's not fully a memory leak, but a something about it.
I'm not sure about my fixes and it will be great if you review them.
The problem appears when I have many similar requests in many fragments which of them has its own instance of SpiceManager. So when I come from one fragment to another, I stop previous instance of SpiceManager and start another one. But some requests are the same, so they get aggregated.

First commit solves the problem when mapRequestToRequestListener in RequestProcessor and RequestProgressManager grows. Because so listeners are not added to map, they are not removed later on stopping.
In log I could see something like:
09-23 20:51:33.840: V//DefaultRequestListenerNotifier.java:131(11926): 20:51:33.858 main Notifying 191 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@4311bb30
I'm not sure that I created right version of HashSet, maybe synchronized version will be better.

Second commit solves some problem with closures. Firstly, I don't see any necessity of second parameter in method notifyListenersOfRequestCancellation() in RequestProgressManager. Listeners list can be acquired from map. Maybe I'm not right and we need to know which listeners need to be notified about cancel?
But the problem is in this code:
final RequestCancellationListener requestCancellationListener = new RequestCancellationListener() {

@Override
public void onRequestCancelled() {
mapRequestToRequestListener.remove(request);
requestProgressManager.notifyListenersOfRequestCancellation(request, listRequestListener);
}
};
request.setRequestCancellationListener(requestCancellationListener);
We put listeners list in RequestCancellationListener that is then put to request itself. So request map in other classes is going to store listeners too, which have much greater size.

I said that it is not clear memory leak because when all requests are executed, the memory usage is dramatically decreased (in my case from 45 to 10 mb), but memory is not cleared in right time.

Thank you in advance for reviewing it.