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 RyanZimmerman87 · Jun 13, 2013 at 04:10 AM · androidiosperformanceempty

Empty public script variables.

Hey guys just have a few quick questions for now, probably will have more later really trying to figure out the details for Unity optimization since I'm still pretty new to programming.

For example, I have an enemy Game Object in my scene that has a general AI script that handles nearly everything that enemy type does and I have about 10-20 of that enemy in a scene. One of those enemies uses a few more public Game Objects or Prefabs within it's script for extra behavior like dropped items, or an object script reference. Nearly all the enemies don't use those extra public script variables and therefor have empty public variables.

So how much of a performance hit is it to have a bunch of objects activated with several empty public variables each? Would it be worth making new scripts for these more unique variables to avoid having empty variables on the majority of the scripts?

I am unsure how much cost this has on memory/load time since they are empty. This is for an Android/iOS project too so I'm especially interested about any Unity performance details.

Comment
Add comment · Show 8
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 iwaldrop · Jun 13, 2013 at 06:27 AM 0
Share

Null references only take up one memory address, so it's a pretty insignificant impact in the grand scheme of things. What will take a lot of memory is if a bunch of your variables actually reference things (then the memory hit is equal to however much ram each loaded object requires).

It's less a unity thing and more a c# thing, but remember to clear your subscribers from events when you're done with them. Whenever an event has a subscriber it prevents the object from being garbage collected, resulting in 'leaked' memory.

In fact, the reson I bring it up is because an even system sounds like it could work quite well for you. With the main enemy driver raising events that the rest of the behavior scripts subscribe to, you could streamline your detection and decision logic into the driver and have different behaviors respond to those events without your master script ever knowing anything about objects it doesn't need to know about. This will remove the mass of 'empty' variables.

avatar image RyanZimmerman87 · Jun 24, 2013 at 02:45 AM 0
Share

Thanks that is very helpful!

So if a bunch of my objects reference a prefab is that a ton of memory addresses, or do they immediately store the entire loaded object into ram?

I was hoping they were just storing the memory reference address and would not load the entire memory of the object unless it was for example Instantiated (like dropped items on an enemy).

I'm not sure if I fully follow clearing the subscribers from events. Preventing memory leaks is a concern for sure but I haven't been able to notice any yet, so I'm not sure if I'm doing it correctly.

Generally I just destroy the entire gameObject whenever I get rid of something from the scene. I am assu$$anonymous$$g that loading a new scene clears all the memory from the last one.

Does anyone have an example of the subscribers being cleared from events that iwaldrop mentioned? I think I'm doing it right, but I'm so new to program$$anonymous$$g it's really impossible for me to know.

Edit: Also am curious for some examples of the main enemy driver. Right now each enemy type has a main script that interacts with other scripts, but it sounds like maybe I'm just including too much stuff in my main script? So ins$$anonymous$$d of loading all of them in automatically in the main script it would be better to call a function within another script and object if and when that behavior actually occurs? So all the enemy item drops might be loaded onto a single gameObject and Script ins$$anonymous$$d of being included on each enemy individually?

Hopefully I'm following you. I need to study more program$$anonymous$$g and watch some more tutorials. Right now I have pretty much designed the entire game and learned program$$anonymous$$g from scratch, so it's really hard for me to know when I'm doing something wrong. I'm not the best in school haha, makes this sort of thing so much harder.

avatar image iwaldrop · Jun 24, 2013 at 02:51 AM 0
Share

For clearing subscribers, just make sure for every += you have a corresponding -=, like so:

 void OnEnable()
 {
     SomeController.SomeStaticEvent += HandleSomeControllerSomeStaticEvent;
 }
 
 void OnDisable()
 {
     SomeController.SomeStaticEvent -= HandleSomeControllerSomeStaticEvent;
 }
 
 void HandleSomeControllerSomeStaticEvent()
 {
     // do stuff when event is received
 }

If you don't do this then even when you change scenes the event will still maintain references to destroyed objects. This may also generate errors when the event is raised, because it tries to call a method which may have null references. Either way, best practice is to do the above (clear every time you subscribe), because you can't be sure the the event you're subscribing to clears it's own refs.

WRT memory, every reference you have (including public GameObject variables assigned through the inspecter) takes memory; even if it's null (as I described above).

avatar image RyanZimmerman87 · Jun 24, 2013 at 03:15 AM 0
Share

Hmm it seems I have much more to take into consideration then. I always thought once you load a new scene all the old stuff got destroyed except playerPrefs.

So for example if I had 10 levels it could be possible that after each level I was leaking more and more memory? So essentially the longer you play the game would become slower?

Does anyone have an example of a common game event which could leak memory? $$anonymous$$aybe quick example of the leak code and then example of way to fix it?

I really have no idea if I'm leaking or not. Everything seems to make sense with the code from my perspective but there could very well be tons of c# or general program$$anonymous$$g stuff that someone like myself would have no idea even exists.

I guess it's worth mentioning though that my levels right now can be replayed after you beat them. I'm not sure if that would make any difference in how you handle this.

avatar image iwaldrop · Jun 24, 2013 at 03:26 AM 0
Share

If you're using pro then check memory in the profiler. If not, watch memory using the platform's monitoring tools. The only way to know for sure if you're leaking memory is to have an empty scene load before you play, record the memory value, then load the same scene at the end of play, and compare the recorded value to the current value. You have to use a 'control' scene so that your measurement isn't invalidated by having different scenes loaded or the same scene in different states.

If you successfully trash all of your references then the memory consumption for your app should be nearly identical in the instances described. Again, events are the most common gotchas because a reference to an object by an event will not allow the garbage collector to purge that object. If that object happens to be a monobehaviour with a ton of additional references then you're talking about potentially leaking a lot of ram.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by umangindianic · Jun 13, 2013 at 04:39 AM

If you have non useful gameobjects or if you use gameobjects only once. SO, you can destroy your gameobject after the use of same public gameobject. For variable make it private. so, it is not use other than your same script and that can not affect the memory use or performance of the game.

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

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

16 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

Related Questions

Mobile game - should I reduce my vertex count? 1 Answer

big terrain performance && mobile 1 Answer

Android heating up, performance questionable for 2d APP. 1 Answer

Performance degradation on Android and iOS switching between scenes 2 Answers

iPad 2 vs Galaxy Tab 2 performance 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