Search Unity

Why is this coroutine causing the memory usage to rise until the machine crashes?

Discussion in 'Scripting' started by Zaine7673, Feb 19, 2019.

  1. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    I have a simple coroutine to display a unity banner when it is ready. When i run the game in the editor the memory continues to rise (from 37%) to 100% until the computer crashes.

    The script works fine except for this one issue.

    Any idea why it is doing this?

    Code (CSharp):
    1. public IEnumerator fetch_banner(string placement_id)
    2.         {
    3.             while (!Advertisement.IsReady(placement_id)){
    4.                 yield return new WaitForSeconds(0.25f);
    5.             }
    6.             Advertisement.Banner.Show(placement_id);
    7.         }
     
    Last edited: Feb 19, 2019
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    What does 'Advertisement.IsReady' do?
     
  3. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    it checks to see if the advertisement is ready. So if the unity servers has returned an ad to display.

    It's part of the unity's advertisements services not something that I've written up.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Are you sure it's that exact script? Ie. if you don't have that coroutine running, the memory doesn't rise?

    If it's caused by that coroutine, there's a bug in the Advertisement API you're using, or you're using it wrong.
     
  5. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    I'm certain that it is that script. Specifically the line

    Code (CSharp):
    1. Advertisement.Banner.Show(placement_id);
    When I comment out this line it's fine.

    The only part of that line that is my own is just the placement_id and i'm sure that isn't the culprit as it's just a string.

    Maybe I'll try a video and see if it causes the same issue...
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    So it seems like you've copied pretty much exactly what's happening here, so the code should be fine, unless there's an internal bug. How often are you showing these ads? Are you ever removing them?
     
  7. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    The script is on one file - Admanager.cs
    It is called once from another file - Menu.cs - from within a Start() method.

    I don't remove the banner on that scene. it is called and the banner is displayed. That's it. very straightforward which is why I'm a little confused.

    I've tried the same script but requesting and displaying a video ad instead and it seems fine. No memory leaks on that. o_O
     
  8. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    could it be because the code is on a separate file/class than the one that it is called in? an instance of that class is created using the new keyword. seems a little strange and limited if that is the case but maybe I'll try bring the code into the menu.cs file and see if that changes anything.

    I really hope the adevertisements API is not limited like that though

    UPDATE: putting all the code in one file doesn't change anything. the memory usage still gradually climbs until it runs out of memory.
     
  9. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    I thought maybe the coroutine continues to run even after displaying the banner so I've tried to put in a debug.log with an integer that increments each time the coroutine is run after the banner is displayed. however, it returns 0 so i assume that it is not continuously running even after it displays the banner.

    I am so lost with this one :-/
     
  10. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Don't call methods that call native functions in coroutines. It's glitchy since some version of Unity between 2018.2.9f and 2018.2.16f.
     
  11. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    Sorry i don't think i fully understand. Are you saying that i should not be using a coroutine for this?
     
  12. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Yes, Advertisement calls into a native (plugin) library. Try just making the calls in something that isn't a coroutine, and see what happens.