How Can Unity Developers Identify and Optimize Lua Performance Bottlenecks?
Lua is widely used in commercial games, but heavier Lua usage can introduce CPU, heap allocation, and memory leak issues. GOT Online's Lua report provides dedicated analysis views to help developers locate expensive functions, trace heap allocations, and investigate potentially problematic Mono object references.
Summary Lua can become an important source of performance overhead as its use increases in a game project. GOT Online provides three key areas for Lua performance analysis: CPU time, heap memory allocation, and Mono object references. CPU analysis helps developers locate expensive Lua functions through call stacks and Lua file, line, and function information. Heap allocation analysis helps identify allocation peaks, continuous allocations, and allocation heavy functions through reverse stack analysis. Mono object reference analysis can help investigate potential memory leaks caused by Lua retaining references to C objects. Why Should Developers Pay Attention to Lua Performance? As game engines, hardware devices, and production standards continue to evolve, GameOptim continues to update its optimization rules and methods and share them with developers. Lua is now widely used in commercial games. As projects make increasingly extensive use of Lua, performance issues can become more noticeable. For example, Lua related functions may appear among the top 20 sources of overhead for logical code in a performance report. When this happens, Lua performance deserves further investigation. So how can developers quickly identify Lua bottlenecks? GOT Online provides a dedicated Lua report with several analysis views designed to help developers investigate CPU time, heap memory allocation, and Mono object references. What Do the Lua Report Sections Show? The GOT Online Lua report provides several views for investigating different types of Lua performance issues. https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/ebf46f09 da1b 4912 995c d62859ddc167.png The three key areas covered in this analysis are: 1. Code Efficiency — CPU Time 2. Heap Memory Allocation 3. Mono Object References Each section focuses on a different type of performance problem. How Can Developers Identify High Lua CPU Time? CPU Cost The CPU Cost page shows the time spent on the Lua side. https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/10404c06 6979 47fc a36d 02f519cfbe36.png By opening individual functions, developers can inspect: Overall execution time call stacks Call stacks for a specific scene Detailed call stacks for a specific frame This makes it possible to quickly narrow down functions that contribute to Lua CPU overhead. https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/9eda8b57 37fa 4cf1 a9ba 3e431bc1d9e2.png The Lua file name, line number, and function name shown in the report can be used to locate the function responsible for CPU time and investigate the cause of CPU time peaks. How Are Lua Functions Identified in the Report? Lua functions use the following naming format: Where: X is the function name. If the name cannot be obtained, it is shown as . Y is the file location where the function is defined. Z is the line number where the function is defined. When a Lua script runs as bytecode, Z is always . Therefore, developers are advised to run Lua from source whenever possible during testing. How Does Reverse Stack Analysis Help Find Lua CPU Bottlenecks? GOT Online also supports reverse order stack analysis. In some cases, developers may need to expand dozens of stack levels to find the function responsible for the largest amount of overhead. This can make the original call stack difficult to inspect. By switching to reverse order analysis, the original CPU time call stack is rearranged so that deeper child functions with higher overhead are highlighted more directly. This helps development teams identify the functions responsible for the largest portion of the overhead more efficiently. How Can Developers Identify Excessive Lua Heap Allocations? Heap Memory Allocation Lua heap allocation is another area that requires attention, particularly when the goal is to reduce the frequency of Lua GC triggers and the overhead caused when GC is triggered. GOT Online uses the cumulative heap allocation curve together with function call stacks to help identify the functions responsible for heap memory allocation. A practical analysis process includes three areas of focus. 1. How Should Developers Identify Heap Allocation Peaks? Start by looking for peaks in heap memory allocation. Large allocation peaks can indicate areas that require further investigation. 2. Why Should Continuous Heap Allocation Be Investigated? Continuous allocation deserves particular attention. If allocations continue to occur on every frame, they should be investigated because persistent allocations can easily trigger GC. 3. How Can Reverse Heap Allocation Analysis Help? Developers can use reverse order analysis to focus on parent nodes that account for a relatively large proportion of the allocation in the overall stack information. This makes it possible to prioritize the functions that contribute more heavily to heap allocation. How Can Developers Locate the Lua Code Behind Large Heap Allocations? The Lua heap analysis also supports reverse order viewing. By switching the viewing method, developers can identify which Lua script and which line of code are responsible for large heap allocations. This provides a direct path from the allocation data to the relevant Lua source code, allowing the development team to locate the corresponding function and make targeted changes. How Can Lua References Contribute to Mono Memory Leaks? Mono Object References Lua plugins commonly use a mechanism in which the C layer maintains a cache containing references to C objects accessed by Lua. This prevents an object from being garbage collected by the C side while Lua may still need to access it. Otherwise, Lua could later attempt to access an object that has already been collected, potentially resulting in a logic error. However, retaining a C object through a Lua reference also prevents that object from being released. As the number of retained references increases, this can contribute to a memory leak on the C side. GOT Online provides a Mono Object References report to help developers investigate this type of issue. The report summarizes the C objects stored in the cache and provides: Object types appearing in the cache The total number of objects for each type The number of destroyed objects when the type inherits from https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/a235d30d 4229 4a8b ac91 bfab9ff0a6ba.png How Can Developers Use Destroyed Object Counts to Investigate Lua Memory Leaks? One simple way to investigate a potential Mono memory leak caused by Lua references is to check whether the Destroyed count is zero. The Destroyed count represents the number of objects that have already been destroyed on the Mono side but are still indexed by Lua. In theory, this number should tend toward zero. If the Destroyed count remains high or continues to increase, there is a strong possibility that the references are contributing to a memory leak. For object types whose counts continue to increase, developers can also compare object references between two different sampling points below the chart. This can provide further information for identifying potentially unreasonable references in Lua. https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/cdc765be 978c 4602 813a c27202389763.png Why Should Memory Leak Testing Run for a Longer Period? When investigating memory leaks, longer duration testing is recommended. A memory leak may not be obvious during a short test. Once the leak occurs repeatedly, however, the retained memory can accumulate over time. Therefore, longer testing periods can make continuously increasing memory usage and object counts easier to observe. How Should Developers Approach Lua Performance Optimization? Lua performance analysis can be divided into three main areas: | Performance Issue | Analysis Area | What to Look For | | | | | | High CPU time | Code Efficiency | Expensive functions, CPU time peaks, call stacks | | Excessive heap allocation | Heap Memory Allocation | Allocation peaks, continuous allocation, reverse stacks | | Potential Mono memory leaks | Mono Object References | Destroyed objects, increasing object counts, retained references | The appropriate analysis method should ultimately be selected according to the actual behavior of the project. GOT Online provides these dedicated Lua analysis capabilities to help developers investigate Lua performance issues from CPU execution, heap allocation, and object reference perspectives. FAQ What Lua performance issues can GOT Online help developers investigate? GOT Online's Lua report covers CPU time, heap memory allocation, and Mono object references. These views help developers investigate expensive Lua functions, excessive heap allocation, and potential memory leaks caused by retained C object references. How can developers find Lua functions with high CPU overhead? In the Code Efficiency CPU Time page, developers can inspect overall, scene specific, and frame specific call stacks. Lua file names, line numbers, and function names can then be used to locate the relevant functions. What does the Lua function format mean? represents the function name, represents the file location where the function is defined, and represents its definition line number. When Lua runs as bytecode, is always . Why should developers pay attention to continuous Lua heap allocation? Continuous heap allocation, particularly when it occurs every frame, can contribute to more frequent GC triggers. Developers can use the cumulative allocation curve and function stacks to identify the functions responsible. How can developers investigate Lua related Mono memory leaks? Developers can examine the Mono Object References report and pay attention to the Destroyed object count. If this count remains high or continues to increase, the Lua references may be contributing to a memory leak. Object references from different sampling points can also be compared for further investigation. New users can get 2 hours of free GOT Online testing time after completing registration. https://www.gameoptim.com/?fopt=blog About GameOptim GOT Online GOT Online is a cloud based performance analysis platform with online performance reports for deeper analysis and collaboration. Based on visualized data display, providing targeted analysis suggestions and performance parameter trends, facilitating efficient quality control execution. To meet a wide range of optimization needs, GOT Online provides four major analysis modes: • Overall Performance Analysis • Mono Heap Memory Analysis • Lua Performance Analysis • GPU Performance Analysis Series Recommendations Why Is Rendering Performance Degrading in Unity Mobile Games? https://www.gameoptim.com/blog/post/RenderingMoudle How Do I Identify and Optimize Animation CPU Bottlenecks in Unity? https://www.gameoptim.com/blog/post/AnimationModule Why Are Particle Systems Causing CPU and GPU Bottlenecks in Unity Mobile Games? https://www.gameoptim.com/blog/post/ParticalSystem Why Is My Unity Game Loading So Slowly on Mobile? https://www.gameoptim.com/blog/post/LoadingResource How can UGUI performance be optimized in Unity across CPU, memory, and GPU bottlenecks? https://www.gameoptim.com/blog/post/UIModule