Life Cycle & Reference Counting | Godot GDScript Tutorial | Ep 1.2

Godot Tutorials
6 Apr 202008:32

Summary

TLDRThis script delves into the concept of variables and memory lifecycle in programming. It explains how variables are storage addresses with symbolic names, and how memory is allocated, used, and released. The script contrasts memory management in C, where variables point to different addresses but can hold the same value, with Godot's reference counting system, which manages memory by tracking object references. It also offers a tip on optimizing memory usage in games and managing nodes in Godot, emphasizing the importance of efficient memory management for performance.

Takeaways

  • 📚 A variable, also known as a scalar, is a storage address paired with a symbolic name that holds a quantity of information.
  • 🔄 The memory lifecycle involves allocation, usage, and release of memory, which is the same across all programming languages.
  • 💾 Memory allocation is managed by the operating system and involves using RAM, which is an array of bytes.
  • 🔍 After allocation, the application can perform read and write operations on the memory, such as assigning and changing variable values.
  • 🗑️ Memory is released when variables are no longer in use, either manually or automatically depending on the programming language.
  • 🛠️ The programming language, not the operating system, determines how memory is handled within an application.
  • 🚀 Using less memory is generally better for performance, especially in games and for users with lower-spec hardware.
  • 🔃 If an application requires more memory than initially allocated, the OS may allocate additional memory from the hard drive or SSD, which is slower than RAM.
  • 🔑 In the C language example, variables X and Y can have the same value but different memory addresses, allowing independent value changes.
  • 🔄 Godot engine uses reference counting for memory management instead of garbage collection, which means objects remain in memory as long as their reference count is greater than zero.
  • 🌐 When a new variable is created and assigned the value of an existing variable in Godot, it points to the same object, increasing the reference count.
  • 🔧 Changing the value of a variable in Godot involves creating a new object and pointing the variable to this new object, which can lead to the previous object being freed if its reference count drops to zero.
  • 📌 Godot nodes are not reference counted, so removing a node from the tree does not free its memory; nodes should be managed carefully to avoid memory leaks.

Q & A

  • What is the definition of a variable in the context of programming?

    -A variable, also known as a scalar, is a storage address paired with an associated symbolic name that contains a known or unknown quantity of information.

  • What is a memory lifecycle in programming?

    -A memory lifecycle refers to the period between an object's creation and its destruction, which includes allocating memory, using memory, and releasing memory.

  • How does a computer's operating system allocate memory to an application?

    -The operating system allocates memory by assigning a portion of the computer's RAM to the application, allowing it to use the memory for read and write operations.

  • What happens when an application is done using allocated memory?

    -When an application is finished using the allocated memory, the operating system releases it, making it available for other applications or for new memory allocation by the same application.

  • How does the programming language influence memory management?

    -The programming language determines how memory is handled, including how it is allocated, used, and released, and whether it is done manually or automatically.

  • Why is it beneficial to use less memory in applications, especially in games?

    -Using less memory is beneficial because it improves performance for users with lower RAM and CPU capacities, ensuring smoother operation and better user experience.

  • What happens if an application uses more memory than initially allocated by the operating system?

    -If an application exceeds the initially allocated memory, the operating system may allocate additional memory from the hard drive or solid-state drive, which is slower than using RAM.

  • Can you explain the basic process of variable assignment in the C language?

    -In the C language, the process involves allocating memory for the variable, assigning a value to that memory address, pointing the variable to that value, and freeing the memory when the variable is no longer in use.

  • How does Godot's reference counting differ from garbage collection in memory management?

    -Godot uses reference counting to determine if an object should be removed from memory based on the number of references to it. This is different from garbage collection, which automatically identifies and removes unused objects.

  • What is the effect of creating a new variable Y that equals an existing variable X in Godot?

    -When creating a new variable Y that equals X in Godot, the new variable Y points to the same object as X, increasing the reference count of the object, but does not create a new object or duplicate the value.

  • How does changing the value of a variable in Godot affect memory management?

    -Changing the value of a variable in Godot may create a new object with a new memory address for the new value, and the variable is then pointed to this new object, potentially leading to the previous object being released from memory if its reference count drops to zero.

  • Why are nodes in Godot not reference counted like other objects?

    -Nodes in Godot are not reference counted because removing a node from the tree does not delete it from memory. To manage nodes effectively, one should create a pool of objects and reuse node objects or explicitly destroy them using 'queue_free' and 'exit_tree'.

Outlines

00:00

📚 Understanding Variables and Memory Lifecycle

This paragraph introduces the concept of a variable, defined as a storage address paired with a symbolic name that holds information. It explains the memory lifecycle, which includes allocation, usage, and release of memory by the operating system. The paragraph uses the analogy of RAM as an array of bytes to illustrate how memory is allocated and used in programming. It emphasizes the importance of efficient memory usage, especially in game development, and how exceeding RAM limits can lead to slower performance due to the use of hard drives or SSDs for memory. The paragraph concludes with a basic example of variable assignment in the C language, demonstrating how memory is allocated, values are assigned, and how separate variables can point to different memory addresses while holding the same value.

05:01

🔄 Godot's Reference Counting Memory Management

The second paragraph delves into Godot's unique approach to memory management through reference counting, contrasting it with traditional garbage collection. It describes the process of creating a variable in Godot, where an object is created, its type and value are set, and a reference count is incremented. The paragraph explains how assigning a new value to a variable results in the creation of a new object and how changing the value of an existing variable updates the reference count of the associated object. It also addresses a common misconception about nodes in Godot, clarifying that removing a node from the tree does not automatically free its memory. The paragraph concludes with advice on managing nodes efficiently by reusing objects from a pool or explicitly destroying them to ensure memory is freed.

Mindmap

Keywords

💡Variable

A variable, also referred to as a scalar, is a fundamental concept in programming that represents a storage address paired with a symbolic name, which can contain a known or unknown quantity of information. In the context of the video, variables are used to store and manipulate data. For instance, the script describes how a variable 'X' is assigned the value 2020, illustrating the basic process of variable allocation and usage in programming.

💡Memory Lifecycle

The memory lifecycle is the duration between an object's creation and its destruction. It is a critical concept in the video as it outlines the stages of memory allocation, usage, and release. The script explains that regardless of the programming language, the lifecycle involves allocating memory by the operating system, using the memory for operations like reading and writing, and finally releasing the memory when it is no longer needed.

💡RAM (Random Access Memory)

RAM, or Random Access Memory, is the primary memory in a computer system where data is stored temporarily while the computer is running. The video script uses RAM to explain how memory is allocated by the operating system for program use. It mentions that RAM can be thought of as an array of bytes, with addresses and values associated with them, which is essential for understanding how variables are stored and accessed in memory.

💡Memory Allocation

Memory allocation is the process by which the operating system reserves a portion of memory for a program to use. In the video, this concept is crucial as it describes how the operating system assigns memory to an application, allowing it to store and manipulate data. The script provides a visual representation of memory allocation, showing how addresses and values are associated in RAM.

💡Read and Write Operations

Read and write operations are fundamental to using allocated memory. The video script explains that once memory is allocated, these operations occur as part of using the memory, such as assigning and changing variable values. For example, when the value of variable 'X' is set to 2020, a write operation is performed to store this value in the allocated memory.

💡Memory Release

Memory release is the process of freeing up memory that is no longer in use by an application. The video emphasizes the importance of releasing memory to make it available for new allocations. It also mentions that memory can be released manually or automatically, depending on the programming language, which is a key aspect of managing system resources efficiently.

💡Reference Counting

Reference counting is a memory management technique used by some programming languages and engines, like Godot, to determine when an object can be safely removed from memory. The script explains that Godot uses reference counting instead of garbage collection, where an object's reference count is increased when a new reference to it is created and decreased when a reference is removed. The object is freed when the count reaches zero.

💡Garbage Collection

Garbage collection is an automated process that some programming languages use to manage memory by identifying and freeing objects that are no longer in use. In contrast to Godot's reference counting, the video script explains that garbage collection is not used in Godot, which instead relies on reference counting to manage memory.

💡Nodes in Godot

In the context of the Godot game engine, nodes represent the fundamental building blocks of the scene graph. The script clarifies that nodes in Godot are not reference counted, meaning that removing a node from the tree does not automatically free it from memory. This is an important distinction for developers to understand when managing resources in Godot.

💡Memory Management

Memory management is the process of controlling the allocation and deallocation of memory in a computer system. The video script discusses various aspects of memory management, such as manual and automatic memory release, and the use of reference counting in Godot. It also provides tips on efficient memory usage, emphasizing the importance of freeing memory to improve performance, especially for games running on systems with lower RAM and CPU capacities.

Highlights

A variable is defined as a storage address paired with a symbolic name that holds a quantity of information.

Memory lifecycle involves allocation, usage, and release of memory by the operating system.

All computers have RAM, which is also known as random access memory.

Memory allocation is performed by the operating system for program usage.

Read and write operations are fundamental to using allocated memory.

Memory is released when variables are no longer in use, either manually or automatically.

The programming language determines how memory is handled, not the operating system.

Using less memory is generally better for performance, especially in games.

If an application exceeds its allocated memory, the operating system may use the hard drive for additional memory, which is slower.

The basic variable process involves allocation, assignment, pointing, and memory release.

In C language, variables like X and Y can have different memory addresses but the same value.

Godot uses reference counting instead of garbage collection for memory management.

Godot increases an object's reference count when a new variable points to it.

Changing a variable's value in Godot may involve creating a new object and adjusting reference counts.

When the reference count drops to zero, Godot releases the object from memory.

Nodes in Godot are not reference counted, so removing a node from the tree does not free memory.

To manage nodes in Godot, create a pool of objects and reuse them or explicitly destroy nodes.

Transcripts

play00:01

let's go ahead and take a look at the

play00:04

dictionary definition of a variable a

play00:06

variable also called a scalar is a

play00:08

storage address paired with an

play00:10

Associated symbolic name which contains

play00:13

a known or unknown quantity of

play00:15

information to better understand what a

play00:17

variable is we need to first learn

play00:19

lifecycles so what is memory lifecycle

play00:22

basically a memory lifecycle is the time

play00:24

between an object's creation and its

play00:27

destruction

play00:27

no matter the programming language

play00:29

memory life cycles are the same you

play00:32

first allocate memory then you use

play00:34

memory lastly you release that memory so

play00:36

you can allocate new memory the first

play00:38

step is allocating memory memory is

play00:41

allocated by the operating system which

play00:43

allows your program to use it all

play00:45

computers have memory also known as RAM

play00:48

it's also referred to as random access

play00:50

memory memory can be thought of as an

play00:53

array of bytes let's go ahead and take a

play00:55

visual look at this as you can see here

play00:57

on one side we have address memory

play00:59

location and the other values this is

play01:02

what it looks like more or less you have

play01:04

addresses on one side and a value

play01:06

associated to the address on the other

play01:09

after the operating system has allocated

play01:11

memory to the application you can start

play01:13

using it read and write operations are

play01:15

happening as you are using the allocated

play01:17

memory such as assigning and changing

play01:20

variable values in your script lastly

play01:22

when you are done using the memory the

play01:24

operating system will release the memory

play01:26

when variables are no longer in use you

play01:29

can release the entire memory so that it

play01:31

is freed and can be used to allocate new

play01:33

memory this can be done manually or

play01:36

automatically depending on the

play01:37

programming language you are using one

play01:40

thing to keep in mind is that the

play01:41

operating system only determines how

play01:43

much memory is allocated to your

play01:45

application however your applications

play01:48

programming language determines how

play01:50

memory is handled one tip I have for you

play01:53

on memory usage is that the less memory

play01:55

you use the better it is in general for

play01:58

games using less memory takes into

play01:59

consideration the performance for users

play02:02

who are using lower size Rams and CPUs

play02:05

in their computers one thing to note is

play02:08

that if there is enough space the

play02:10

operating system will allocate more

play02:11

memory from RAM to your application if

play02:14

you're up early

play02:14

ocation uses more than the allocated

play02:16

memory that was previously assigned to

play02:19

it by the operating system if you go

play02:21

over the RAM limit the operating system

play02:23

will start allocating memory to your

play02:25

application from the hard drive or

play02:27

solid-state drive one thing to note is

play02:29

that using hard drive or solid-state

play02:32

drive for memory as slower than using

play02:34

Ram let's take a look at the basic

play02:37

variable process we have a variable X

play02:39

which equals the literal integer 2020

play02:43

first the application will allocate

play02:45

enough memory for the variable in this

play02:47

case enough memory to hold an integer

play02:49

second the application will assign the

play02:52

value 20/20 to that memory address

play02:55

location third the application will

play02:57

indicate to the variable X that it needs

play02:59

to point to that value lastly depending

play03:02

on the language memory is freed when not

play03:05

in use let's go ahead and see this

play03:07

visually with the C language we have an

play03:09

integer X which equals the literal

play03:12

integer value 2020 as you can see here

play03:15

we have our integer X which has been

play03:17

assigned a memory address location and

play03:20

it has also been assigned its value 2020

play03:24

so basically the memory address location

play03:27

has the value of 20/20 let's go ahead

play03:30

and create a second integer Y which will

play03:32

equal X as you can see here X still has

play03:35

the same location and the same value

play03:38

however the application has created a

play03:40

new location memory address for Y

play03:42

however it's still assigned the same

play03:44

value as X keep in mind that the

play03:47

application has created different

play03:48

location addresses for x and y however

play03:52

the values are the same for x and y

play03:54

let's go ahead and modify the value of y

play03:57

as you can see here we have now assigned

play04:00

the literal integer 0 to the integer Y

play04:04

notice here that the location for Y

play04:07

remains unchanged however the value has

play04:11

now been modified to 0 you can change

play04:14

the value of Y or the value of x as many

play04:16

times as you want you will never change

play04:19

location address in memory for either of

play04:23

these values the way the C language

play04:25

handles memory is a good thing as ad

play04:27

couple

play04:28

the memory address of X&Y meaning that

play04:31

you can change the values of X without

play04:33

changing the values of Y and you can

play04:36

change the values of Y without changing

play04:38

the value of X this is possible because

play04:41

both x and y are pointing to two

play04:45

different memory address locations in

play04:48

the application now that we have a basic

play04:50

understanding of how the C language

play04:52

handles memory let's go ahead and take a

play04:55

look

play04:57

gadot uses reference counting what this

play05:01

means is that instead of using garbage

play05:02

collection to remove from memory unused

play05:05

code Godot uses reference counting to

play05:08

determine whether or not something

play05:10

should be removed from memory let's go

play05:12

ahead and take a look at how Godot

play05:14

handles the execution of this line of

play05:17

code so we have a variable X which is

play05:20

equal to the literal integer 20/20

play05:22

what Godot does is it creates an object

play05:25

then it sets the type to integer next it

play05:29

sets the value to 20/20 then it creates

play05:32

a name called X it points X to the new

play05:36

object and then it increases the objects

play05:40

reference count by one as you can see

play05:42

here the memory layout is different than

play05:45

the memory layout previously shown with

play05:48

the language c instead of x owning the

play05:51

block of memory where the value 20/20

play05:53

lives in the newly created object owns

play05:56

the memory where 20/20 lives the name X

play05:59

doesn't own any of the memory address

play06:03

since Godot uses reference counting

play06:06

instead of garbage collection what this

play06:08

means is that as long as the object has

play06:10

a reference count greater than zero the

play06:13

object will continue to exist in memory

play06:15

let's go ahead and create a new variable

play06:17

Y which will equal X as you can see here

play06:21

our application goes through the same

play06:23

process it went through to create the

play06:24

value of X however instead of creating a

play06:27

new object what our application did in

play06:30

this case what good dul did is that it

play06:32

created the name Y and it pointed it to

play06:35

the object that has the value 20/20

play06:38

basically it points the variable Y to

play06:41

whatever X was pointing to as you can

play06:44

see here the object now increases its

play06:47

reference count by 1 but what happens in

play06:49

memory when we assign a new integer

play06:52

value to y1 this case the application or

play06:56

rather Godot creates a new object with a

play07:00

new address memory to contain that new

play07:02

value then what it does is it takes the

play07:04

name of Y and it points it to that

play07:07

object so while X is still 20/20 or

play07:11

why is now zero let's go ahead and take

play07:14

a look at what happens when we change

play07:15

the value of x as you can see here our

play07:19

application gadot creates a new object

play07:21

with a new memory address it creates the

play07:25

type it creates its value it points X to

play07:28

this object and it increases its

play07:30

reference count by one as you can see

play07:32

here X is pointed to this object Y is

play07:35

pointed to another object but nothing is

play07:38

pointing to our previous object which is

play07:40

20/20 as you can see here the reference

play07:42

count is now zero what this tells the

play07:45

application Godot to do is that it needs

play07:48

to release this object from memory this

play07:51

is basically a godot's way of memory

play07:53

management one tip I have for you is

play07:56

that nodes in Godot are not reference

play07:59

counted in the engine what this means is

play08:02

that removing a note from the tree will

play08:04

not delete it many people will use

play08:06

remove child thinking it removes it from

play08:09

memory however this is not the case

play08:11

to better manage nodes create a pool of

play08:13

objects and reuse the node object you

play08:17

can also explicitly destroy the note

play08:19

from memory by calling Q free and the

play08:21

exit tree which is called when the node

play08:23

is removed from the tree

Rate This

5.0 / 5 (0 votes)

Related Tags
Variable BasicsMemory ManagementProgramming ConceptsRAM UsageC LanguageReference CountingGodot EngineMemory AllocationData StoragePerformance Tips