- Home /
UnityWebRequest huge cpu cost
Hi
I've seen similar issues to this pop up a few times, but nothing with any clear solution. I'm making quite a few UnityWebRequests at a certain point in my game - downloading a varied set of objects/textures from a server. After executing the requests, the CPU usage of the editor goes way up high, and never comes down again.
There has been some discussion of a bug to do with quitting when requests aren't finished, but I'm in 5.2.2 here, and I'm seeing the huge cost anyway.
Has anybody else got this? Am I supposed to destroy the web requests potentially? This is an example of my code to GET a request:
public UnityWebRequest GetWebRequest(string url, WebRequestCallback callback, object user_data = null)
{
UnityWebRequest web_request = new UnityWebRequest(url, "GET");
web_request.downloadHandler = new DownloadHandlerBuffer();
if (callback != null)
{
PendingWebRequest pw = new PendingWebRequest();
pw.m_request = web_request;
pw.m_user_data = user_data;
pw.m_callback = callback;
m_web_request_queue.Enqueue(pw);
}
web_request.Send();
return web_request;
}
Basically the request is submitted then placed into a queue of active requests, which is monitored elsewhere. When the request at the front of the queue is completed, the callback is triggered:
//hand any completed requests to the central server
int csreqcount = m_web_request_queue.Count;
for (int i = 0; i < csreqcount; i++)
{
//dequeue pending request and check if done
PendingWebRequest pr = m_web_request_queue.Dequeue();
if (pr.m_request.isDone)
{
//done so handle it
pr.m_callback(pr.m_request, pr.m_user_data);
}
else
{
//not done so requeue it
m_web_request_queue.Enqueue(pr);
}
}
I see nothing unusual in my code. But I have narrowed down the CPU cost to the web requests and see no way of reducing them
On the order of 100. To be clear though, I'm not concerned about the fact that a few hundred requests might be a little spikey. The issue is that the cpu spinning is permanent - once they're executed unity is chomping 100% of cpu until editor is restarted.
Try doing a few requests until you start noticing lag, then delete all references to any request, then call the garbage collector. Does the lag clear?
Not as far as I can tell. I figured in theory there may be some lost reference to something somewhere, but if there is I can't see it. Are you thinking I'm just sitting there with 100s of open/persistent http requests that have a reference to something somewhere?
Well, update on this. I still haven't worked out the problem, though I have discovered it is highly unpredictable. The exact same code never causes the issue if fired off from the editor, and is not predictable when fired off from in game - sometimes it'll only take a couple of requests, other times it'll take a few 100. Eventually though Unity is reliably spinning at around 90% CPU use.
Time to give in and just use .net's version?
If it doesn't happen in the editor, how do you know the requests are causing the CPU cost?
Have you checked the log files for errors?
Your answer
Follow this Question
Related Questions
UnityWebRequest Post Not Returning Anything 1 Answer
Post Requests without Coroutines 2 Answers
UnityWebRequest - 403 Forbidden 0 Answers
How to use chunked transfer in UnityWebRequest? 0 Answers
JSON WebRequest works in 2018.2, throws 403 error in 2018.3 0 Answers