How Can Developers Identify and Optimize Lua Memory Issues in Unity?
Lua is widely used for hot updates, but extensive use can introduce runtime memory issues, including excessive allocations and unreclaimed references. GameOptim GOT Online provides Lua memory analysis to help developers examine overall heap usage, locate allocation-heavy Lua functions, and investigate persistent C# object references.
About GameOptim GameOptim helps Unity developers identify memory issues, rendering bottlenecks, and performance regressions through automated profiling and cloud based performance analysis. Explore more: 🌐 Website: www.gameoptim.com https://www.gameoptim.com/?fopt=blog 📘 Blog: www.gameoptim.com/blog/ https://www.gameoptim.com/blog/ 💼 LinkedIn: www.linkedin.com/company/gameoptim/ https://www.linkedin.com/company/gameoptim/ 🎥 YouTube: GO.PerformanceLab https://www.youtube.com/@GO.PerformanceLab 💬 Discord: GameOptim https://discord.gg/4Jh6hj9gRw ⭐ GitHub: GameOptim https://github.com/GameOptim/unity mobile performance guide 💻 Dev: GameOptim https://dev.to/gameoptim Summary Lua can introduce runtime memory bottlenecks as its use increases, particularly through excessive memory allocation and unreclaimed references. GOT Online's Lua memory analysis provides three areas of analysis: overall heap memory, detailed heap allocations, and Mono object references. Developers can use heap allocation data to identify Lua functions that allocate large amounts of memory and investigate potential GC overhead. Mono object reference analysis helps identify C objects retained through Lua side references. The analysis currently supports uLua, ToLua, SLua, and xLua. Why Can Lua Become a Runtime Memory Bottleneck? Lua is widely used by development teams to implement code hot updates because it provides flexibility for adding features and fixing bugs. However, as Lua becomes more extensively used within a project, its memory behavior can become an important runtime performance concern. Two issues are particularly relevant: excessive memory allocation and memory that cannot be properly reclaimed. To help developers investigate these issues, GameOptim provides Lua memory analysis through GOT Online. After completing a real device test and uploading the test data, developers can examine Lua memory behavior through the analysis report. What Does GOT Online Analyze in Lua Memory? GOT Online's Lua memory analysis is divided into three areas: 1. Overall heap memory 2. Detailed heap allocations 3. Mono object references Together, these views provide different levels of information, from overall Lua memory trends to specific allocation functions and C objects retained through Lua references. How Can Developers Monitor Overall Lua Heap Memory? The overall heap memory page records Lua's total memory trend along with the number of several important Lua object types: Table Userdata Function Developers can inspect Lua memory usage at a specific frame during the test and compare the observed value with the expected memory behavior of the project. This makes it possible to identify memory usage that may require further investigation, including potential memory leak risks. How Can Developers Find Lua Functions That Allocate Large Amounts of Memory? The detailed heap allocation page is designed to help development teams locate Lua functions associated with larger heap allocations. Developers can inspect the allocation stack at a specific frame to understand where Lua heap memory is being allocated. This information can then be used to optimize Lua memory allocation and reduce both the frequency of Lua GC triggers and the overhead when GC is triggered. How Is a Lua Function Identified in the Allocation Data? Lua functions use the following naming format: Where: X is the function name. If the function name cannot be obtained, it is shown as . Y is the file location where the function is defined. Z is the ending line number of the function definition. When Lua scripts run as bytecode, this value is always . Therefore, when performing the test, it is recommended to run the Lua scripts from source whenever possible. How Can Developers Locate the Specific Lua Code Causing Large Allocations? Similar to reverse order analysis of Mono heap memory, Lua heap memory can also be inspected in reverse order. By switching the viewing mode, developers can identify which Lua script and which line of code are responsible for larger heap allocations. This provides a more direct path from the allocation data to the corresponding Lua function and source code, allowing the development team to locate the relevant code and make targeted changes. Why Can Lua References Cause C Memory to Remain Allocated? Lua plugins commonly use a mechanism in which the C layer maintains a cache containing references to C objects accessed by Lua. The purpose of this cache is to prevent a C object from being garbage collected while Lua may still access it. Otherwise, a later Lua access could encounter an object that has already been reclaimed by the C garbage collector, potentially resulting in a logic error. However, retaining C objects through these references also means that the objects cannot be released while the references remain. If the number of such references continues to increase, they can contribute to memory leakage on the C side. How Can Developers Identify C Objects Retained by Lua References? GOT Online provides a Mono object reference report to help investigate this type of issue. The report summarizes the C objects maintained in the relevant cache and provides: Object types appearing in the cache The total number of objects for each type The number of objects that have already been destroyed when the type inherits from Developers can click an individual object type to inspect how its total count changes over time. If the count of a particular object type continues to increase, developers can compare object references between two different sampling points to further investigate potentially unreasonable references originating from Lua. Which Lua Frameworks Are Supported? The current Lua memory analysis supports several commonly used Lua frameworks: uLua ToLua SLua xLua If a development team recompiles the Lua Native library with a custom name, the library name can be specified through the interface. 💡 New users can get 2 hours of free GOT Online testing time after completing registration. https://www.gameoptim.com/?fopt=blog FAQ What Lua memory issues can GOT Online help developers investigate? GOT Online's Lua memory analysis covers overall Lua heap memory, detailed heap allocations, and Mono object references. These views can help developers investigate excessive Lua memory allocation and C objects retained through Lua references. How can developers find which Lua function is allocating memory? The detailed heap allocation analysis allows developers to inspect allocation stacks at specific frames and identify Lua functions associated with larger heap allocations. Can developers locate the specific line of Lua code responsible for an allocation? Yes. By switching the Lua heap memory viewing mode, developers can inspect the allocation data in reverse order and identify the Lua script and line associated with larger allocations. Why should Lua source code be used instead of bytecode during testing? When Lua scripts run as bytecode, the ending line number in the Lua function naming information is always . Running Lua from source whenever possible provides more useful location information during analysis. How can Lua references contribute to C memory leaks? Lua plugins may maintain C object references in a cache to prevent objects from being garbage collected while Lua still needs them. If these references continue to accumulate, the corresponding C objects may remain unreleased and contribute to C memory leakage. 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