Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NeoKuro · Feb 02, 2015 at 08:19 AM · c#newmemory managementmemory-leak

Does the use of new VectorX within a loop cause a memory leak?

So previously I had an issue in C++ where using 'new Something' within a loop or function that is repeatedly called would cause a memory leak (as I never deleted/destroyed it after it was finished). I was wondering if the same thing happens in Unity with vectors.

As it is easier for me to define a new vector all at once using;

 Vector3 myVector = new Vector3 (X, Y, Z);

In my code I have this within a for loop (as well as several other constantly called functions including update) and was wanting to check whether it will cause a memory leak, or if Unity automatically deletes the 'new vector3...' once its done (since it wasn't an actual variable).

Also, within things like Visual Studio, I can easily tell iftheres a memory leak as when debugging the VS creates a new process for running the debugger, so I can easily see in task manager whether or not I have a memory leak. But in Unity I'm uncertain if this is also the case (IE if a leak happens, the memory usage for 'Unity3d.exe' will constantly increase) or if it will remain constant, or if there is another way to determine if there is a memory leak (even a tiny one)

I apologize if this was asked before, but no suggestions came up answering my question(s).

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
Best Answer

Answer by karljj1 · Feb 01, 2015 at 11:22 PM

.Net is a managed language, it looks after the memory. Objects are automatically freed when they are no longer needed by the application where in C++ you must free the object yourself using delete. So no you should not get a memory leak HOWEVER in Unity some objects are not managed and must be destroyed manually or by forcing Unity to do it for you. Fortunately Vector is not one of them :)

Comment
Add comment · Show 1 · 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 fafase · Feb 02, 2015 at 08:21 AM 0
Share

On top of that, Vector is a structure so they are added to the stack and "destroyed" when out of scope. Actually, they are not really destroyed, the stack pointer is just moving down and the old data will be overwritten. So your loop is not doing much to the Heap memory go ahead and use it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I have this out of memory issue when running my webgl game in iOS. 0 Answers

how can i auto target Gameobject on startup 1 Answer

Making player character walk slower when further from centre 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