Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hrgchris · Mar 15, 2017 at 05:10 PM · webhttprequest

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

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lgarczyn · Mar 15, 2017 at 06:28 PM 0
Share

How many requests are we talking about here?

avatar image hrgchris · Mar 15, 2017 at 06:54 PM 0
Share

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.

avatar image lgarczyn hrgchris · Mar 15, 2017 at 09:47 PM 0
Share

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?

avatar image hrgchris lgarczyn · Mar 16, 2017 at 04:24 PM 0
Share

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?

avatar image hrgchris · Mar 16, 2017 at 05:05 PM 0
Share

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?

avatar image lgarczyn hrgchris · Mar 16, 2017 at 08:06 PM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges