• 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 ThumbStorm · Dec 13, 2010 at 05:09 PM · crashmemory

Problems with large script

Hello, I was having a problem with unity crashing because it was running out of memory and traced it back to a script that has over 2000 lines of code in it. So I broke the script into several scripts to fix this. Not all of the lines are used at once, the way the script works is a function is called at the end of the start function as well as an integer it then uses if statements to find which block of code to use and then only needs about 100 - 200 lines of code to execute. My question is if anyone knows a max number of line you should use in one function? thanks

Comment
Add comment · Show 1
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 Proclyon · Dec 13, 2010 at 05:22 PM 0
Share

Well I have not ever heard of a problem in script size, code minimization for that purpose , nor for commenting. I can't imagine the size EVER being the problem. But I could just be totally wrong of course, it's not like I coded for 43 years or something silly. But really when u reach that number (and me being a hypocrit with my 1.3k script) I'm splitting it, and you have a 95% chance of getting a good readability increase by doing so aswell

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by michaelvoigt · Dec 13, 2010 at 05:19 PM

Javascript or C#?

I have used monolithic scripts at 4k lines and they work fine, I don't think there is a limit.

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 ThumbStorm · Dec 13, 2010 at 05:22 PM 0
Share

I'm using JavaScript.

avatar image
0

Answer by Mike 3 · Dec 13, 2010 at 05:19 PM

It shouldn't matter how many lines it is, lines don't really attribute to memory use, new objects do. If it's throwing memory exceptions, you're probably doing something pretty dodgy inside the code itself.

Comment
Add comment · Show 3 · 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 ThumbStorm · Dec 13, 2010 at 05:31 PM 0
Share

Once I broke up the code into 6 functions ins$$anonymous$$d of 1 function with over 2000 lines of code it executes the function in a split second ins$$anonymous$$d of 30 and doesn't run out of memory and crash any more. The only thing I came up with is it reads through every line in the function regardless of what variable is being sent. But I am pretty new to scripting so I wouldn't throw out the possibility of dodgy code.

avatar image Mike 3 · Dec 13, 2010 at 05:35 PM 0
Share

One function with 2000 lines is slightly different, yeah - what is probably happening is that it's not deallocating the memory for any of the stuff you're using until after the function has ended (which is right), splitting it up means that it'll be able to garbage collect after each snippet function. Still, you must be doing something pretty scary to max the memory usage in a single function

avatar image Mike 3 · Dec 13, 2010 at 05:37 PM 0
Share

One other thing - it'll probably fun just as fast split up into seperate functions in the same script too. Either way - it's still not lines of code, but allocations done at a certain scope

avatar image
0

Answer by Statement · Mar 13, 2011 at 03:38 AM

My question is if anyone knows a max number of line you should use in one function?

Well, this isn't a hard limit but more a practical limit for developers; I tend to avoid methods larger than 30 lines like the plague. Instead I often refactor code. This isn't because the program will run slow, but because I will code slowly if it was a huge "mess". I guess it's part subjective and part wisdom. It can become quite hard maintaining larger blocks of code. I guess everyone has their "pain thresholds" at some number of lines of code. Which is yours?

In fact, breaking into several functions cause a slight overhead but I don't try to squeeze out every single penny of my performance since I'd be working so much slower. I leave optimizations to things that matter. Reducing time/memory complexity, caching, and tightening out loops where I need them. Should I really need performance and I know it's because of these extra functions then I might opt to put them back inline again.

Readability is clearly improved if a part of the function says "UseJetpack()" instead of having several extra variables in the same scope, calling animations, decrementing fuel, playing sounds and so on.

It also be easier to cope with the relationship of variables if they are in clearly defined scopes. You know that a local variable in function X won't impact a local variable in function Y directly. This is also one of the reasons I limit my blocks of code to a handful.

Generally my functions are between 1 to 10 lines of code, but it's down to preference in my case. I usually create new functions if a method starts to grow out of hand. Refactoring in Visual Studio is so easy it becomes second nature.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Unity crashes when GUI becomes too complex 1 Answer

Crash During Builds + Recommended Size of Resources Folder 0 Answers

Crash on Inspector during play 0 Answers

Limit on the amount of data I can store on Android Devices? 0 Answers

[iOS Memory Question] At ~35MB of memory on a heavily multitasked iPhone4/iPad we are getting suspend-crashes after lowmemory warnings. Is there anything we can do? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges