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 bhaskar1234 · Jul 16, 2017 at 02:03 PM · memorymemory-leakmemory leak

Memory Leak caused by Multi Threading

I am creating a board game similar to tic tac toe, and I've created an AI to play that game. The AI is very CPU intensive, so I decided to put it on it's own thread. I'm using this plugin to do multithreading: https://www.assetstore.unity3d.com/en/#!/content/15717 .

I have this IEnumerator:

 static IEnumerator executeAITurn(Turn turn) {
     Vector2[] move = mctsManager.mcts(new State(sections, null, turn), AIIterations)[0, 0].metaData.lastMove;

     yield return Ninja.JumpToUnity;

     input(move, true);

     yield return Ninja.JumpBack;

     Debug.Log("DONE!");
 }

and I run it using

 gameManager.StartCoroutineAsync(executeAITurn((AITurn == Turn.X) ? Turn.O : Turn.X));

Normally when I run executeAITurn it works normally without problems, but for some reason sometimes when I run it, it does what it's supposed to but in task manager my memory just increases by 200 mb, and stays there. After that as I continue running executeAITurn the memory just keeps on increasing to like over 1000 mb, and then gets extremely slow.

Another thing is that if I click the play button again to stop the game, the memory still stays that high.

Any help would be appreciated.

EDIT:

mctsManager Class: https://pastebin.com/yzeHrY2p

input Function: https://pastebin.com/8f2hzZws

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · Jul 17, 2017 at 01:33 PM

You are constantly creating new states: new State(sections, null, turn) which creates a new object everytime and cause memory to heap up.

Instead you could create one state, and edit the state the next time. Like so:

  static State stateData = new State();
  static IEnumerator executeAITurn(Turn turn) {
 
 //change existing state
 stateData.Sections = sections;
 stateData.SecondVar = null;
 stateData.Turn = turn;
 
      Vector2[] move = mctsManager.mcts(stateData, AIIterations)[0, 0].metaData.lastMove;
      yield return Ninja.JumpToUnity;
      input(move, true);
      yield return Ninja.JumpBack;
      Debug.Log("DONE!");
  }

That might have some performance improvement. Other than that you're not providing enough information for us to see what is causing this. For example what is your input method what does mctsManager.mcts do? etc..

Comment
Add comment · Show 6 · Share
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 bhaskar1234 · Jul 17, 2017 at 01:46 PM 0
Share

I added the code for mcts$$anonymous$$anager and the input function to question.

avatar image bhaskar1234 · Jul 17, 2017 at 01:50 PM 0
Share

After looking into mcts$$anonymous$$anager I'm noticing that I do use new State() and new Section[] a lot. So is the solution to make single variables for all those situations, that I just change when I need to?

avatar image ShadyProductions bhaskar1234 · Jul 17, 2017 at 04:34 PM 0
Share

It's always going to be better, but not sure if the difference in performance will be huge. It's hard to say where it's co$$anonymous$$g from, have you profiled the game using the unity performance profiler, you can see where the spikes are co$$anonymous$$g from.

avatar image bhaskar1234 ShadyProductions · Jul 17, 2017 at 05:12 PM 0
Share

I used the profiler, but I'm not sure how to use it to track where the spikes are co$$anonymous$$g from. like I see the total memory allocated slowly increasing, but I'm not sure how to find the cause for it using the profiler.

Show more comments

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

69 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

webgl memory leak 1 Answer

Online, Possible Memory Leaks with RPC 0 Answers

RenderTexture with Video Player 0 Answers

Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers

Any Known Memory Leak issues in 3.5.7f6? Android/iOS 1 Answer


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