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 eem · May 16, 2011 at 04:24 AM · iosmemorymanagementallocation

Don't allocate memory in Update loops on iOS. - How strictly should this be followed?

I read in the manual that its best to avoid memory allocations in Update functions. I understand what this means generally, don't create new textures every update, don't instantiate objects every update. But I wonder how intensely must you adhere to this?

Like for example I tend to do alot of

Vector3 newPos = transform.position; newPos.y += Time.deltaTime; transform.position = newPos

Should I not be doing something like that in update? Should I make like a temporary 'globalUtilityVector3' outside of the update loop, so then I don't have to create any temporary Vector3's to read in and out data?

I also do alot of

GUI.TintColor = new Color(1,1,1,.5);

should I not do that either? Should I declare a 'globalUtilityColor' outside of Update and then go

globalUtilityColor.a = .5; GUI.TinColor = globalUtilityColor;

?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jean-Fabre · May 16, 2011 at 04:49 AM

Hi,

You could/should store newPos , Color infos, etc in variables yes, and that would avoid creating it on each update. I don't know what kind of standard you should follow to write them but I use something like _newPos_ or _tintColor_ and I then know that them variable are simply here to avoid memory allocation and really do not hold anything relevant.

EDIT: It seems that this is not important after all given all the comments, but without a proper bold statement from Unity in their docs ( haven't find one, but willing to learn!), I still prefer avoiding creating new variables inside function, when the function is called a lot during your game loop most likely). It might be unnecessary, yes, but I feel better like that with my current experience.

Bye,

Jean

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 Jesse Anders · May 16, 2011 at 02:47 PM 0
Share

Are you saying that with iOS it's important even to avoid creating objects that would typically be stack-allocated? (Just curious.)

avatar image eem · May 22, 2011 at 09:36 PM 1
Share

I was watching the memory on the iPad and the growth of memory allocation was well within the garbage collectors control. So technically you probably don't need to worry about it. BUT I am personally kind of obsessive with such things. I mean doesn't it take the gargbage collector extra cycles to cull things from memory? So if you can avoid the garbage collector wouldn't you get better performance, even if its small?

avatar image UltimateBrent · May 22, 2011 at 09:59 PM 0
Share

I think eem is right. It's the sort of thing that if you have it on every enemy in your game, that little bit could multiply out to big savings if you have a ton of enemies.

avatar image Peter G · May 22, 2011 at 11:48 PM 0
Share

@Jesse Anders, Unity actually encourages that you use Stack Allocated objects simply because they don't increase the total memory allocation. So you can use them all you want.

avatar image Jesse Anders · May 23, 2011 at 02:57 AM 0
Share

@Peter G: I know all that. I was just wondering why $$anonymous$$ was suggesting that creation of stack-allocated objects should be avoided (which doesn't seem to make much sense, for the very reasons you've mentioned).

Show more comments
avatar image
1

Answer by Peter G · May 22, 2011 at 11:47 PM

Actually, the Vector3 and globalUtilityColor won't do anything to your memory allocation. Since they are stack allocated, you will see zero memory allocation from them.

Rule of thumb:

  • It is ok to allocate structs such as Vector3 in your functions. They won't add any memory consumption.

  • Don't allocate any classes such as Transform during a function.

I would say if you are debating creating a struct locally, DO IT. It will give you cleaner code. Looking at a long list of global variables that are only used inside of one specific function will confuse anyone who looks at your code.

Comment
Add comment · Show 4 · 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 Jesse Anders · May 23, 2011 at 02:55 AM 0
Share

Actually, a statement such as 'newPos = transform.position' shouldn't allocate any (non-stack-allocated) memory, I don't think. No new object is created; all that happens is that a reference to an existing object is returned. ($$anonymous$$aybe I'm misunderstanding your post though.)

avatar image Peter G · May 23, 2011 at 05:46 PM 0
Share

@Jesse Anders, thanks for catching that. I don't know why but I had always thought that Component looks up allocated memory. I just tested it and you were correct. Thanks.

avatar image eem · May 24, 2011 at 09:45 AM 0
Share

What about newPos = new Transform()? That will allocate memory won't it?

Is there a list somewhere that would tell me what things go on the stack vs the heap? Do Rects go on the stack? And why doesn't the stack increase memory allocation? Does C# go through your entire app and figure out how much memory the stack could expand to and allocate that before hand or something like that?

avatar image Peter G · May 24, 2011 at 07:25 PM 0
Share

@eem, yes you would never create a Transform by directly calling the constructor, but in theory that would allocate memory on the heap.

If it is a struct/value type, then it is put on the stack. Reference types and classes go on the heap.

With the stack allocation, among a number of other things, functions can put values on top of the stack, but they are responsible for removing it when the function returns so the final change will be zero.

This page is kind of a stub, but the bottom links to some other similar concepts. http://en.wikipedia.org/wiki/Stack-based_memory_allocation

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

1 Person is following this question.

avatar image

Related Questions

"stream from disc" WAV/AIFF file allocates tons of memory 0 Answers

Memory usage for the simplest iOS/Android app grows over time, says Unity 3.5 Profiler 0 Answers

Unexpected memory overhead of textures on iOS 0 Answers

Unity3D app crashes out after error "Warning -> ApplicationDidReceiveMemoryWarning' 1 Answer

Unity IOS(IPad2) auto exit app after loaded too many images 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