0

Update:

gc will only work after the final } , which means taht only after the entire for loop has completed will the gc garbage collection be running once. so, is there any programming patter that can be used here? kinda like a for loop but in the mean time also allow gc to run each iteration. I don't know if I'd made myself clear... I can live with the performance dropping caused by gc running each iteration.

I was using volley on a android project and I can't get a working solution.

basically I have a huge collection of stuff that I will send (thru http with volley lib) continuously. when I send 10 of them it's no problem, but if I do it continuously it will eventually eat up all the memory and the app get terminated because of the heat space it taken.

below is the demo code block

requestqueue = Common.getQueue();//it will get a normal requestqueue
for (int i = 0; i < waitinglist.size(); i++) {

    synccopyreq.filedatas.add(Common.getDataFromWaitinglist(i));
    Common.httpPostWithQueue( requestqueue,"tagfullcopy", httpEndPoint+"/sync/fullcopy", gson.toJson(synccopyreq), that, new Common.CallbackInterface() {
        @Override
        public void completed(String result)
        {
            Log.i("shawhu","ok done");
        }
    });
//this was added but it doesn't work unfortunately
//using System.gc() like so was not recommended and most of the time wont'be effective either, I knew that.
System.gc();


}

Please read this, I've tried the profiler, when the whole thing complete (like when dealing with 10 items) the memory usage will go down fine, but if I run it continuously, it won't. the memory usage just go up continuously and it will eventually break the app (terminated by the android system). How do I free memory in every iteration in the loop?

Please help me, thanks!!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
shawhu
  • 1,217
  • 1
  • 12
  • 27
  • If the API, you are sending the requests to, is your own API, then I would suggest taking a single request with all the data at once on API side, instead of series requests. It is not a good practice to depend on the HTTP requests for very frequent requests. – Harshvardhan Joshi Jan 14 '20 at 07:29
  • You can also choose WebSockets for doing this type of series of requests. It's a better choice for you then HTTP requests. – Harshvardhan Joshi Jan 14 '20 at 07:32
  • @HarshvardhanJoshi thanks for your answering, unfortunately each httprequest is huge (several MB in size), and so I won't be able to combine hundreds of them into one. websockets could be a good solution but it's far too much work that we had contracted for. I guess it will be our last resort thing but before that, really hoping that we can find a solution using http+looping structure(actually it's kinda like a constantly running never end loop like a event loop). – shawhu Jan 14 '20 at 08:38

1 Answers1

1

There's no definite way freeing memory manually in the languages which has it's own GC.

However, you can request a GC call in Java using System.gc().

Refer this: https://www.geeksforgeeks.org/garbage-collection-java/

Note: While we can call this method from anywhere at anytime, it still does not guarantee that the garbage collection will run immideately.

I would seriously not recommend using GC like this though, as each GC call may affect the performance on Main Thread

Harshvardhan Joshi
  • 2,855
  • 2
  • 18
  • 31
  • sorry but this was tried. simply calling System.gc() in each loop won't be effective enough. I should modify my question accordingly. thanks for the tip though. – shawhu Jan 14 '20 at 08:53