Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12 Scripting with Lua > Memory Management

MEMORY MANAGEMENT

Like most scripting languages, Lua uses automatic memory management and garbage collection. This works very much like TR1’s shared_ptr construct. Every variable you create is reference counted, and when all references go away, the object is marked for garbage collection. Lua periodically runs a garbage collection cycle where it walks through the garbage list and frees up memory. You don’t have to do this manually; it’s done automatically by Lua. If you do want to force a garbage collection cycle, you can call the collectgarbage() function.


Global Variables Are Here to Stay

image

Global variables are never garbage collected until the program shuts down. They can be accessed by any part of the program at any time, so Lua has no way of knowing when you don’t need them anymore. When you’re done with a global variable, it’s good to assign nil to it. Assuming there are no other references, this will cause Lua to mark it as collectable.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint