SPVM::D...llection(3)

πŸ“¦ version 0.990125, in alpine 3.24

πŸ’– NAME πŸ’–

okay bestie, this is the SPVM::Document::Language::GarbageCollection(3) page from the User Contributed Perl Documentation, and it is literally all about garbage collection in the SPVM language. yes, we are getting into memory vibes. 🫢

🌸 DESCRIPTION 🌸

This document describes garbage collection in the SPVM language. that's the whole assignment, and it ate. πŸ’…

✨ GARBAGE COLLECTION ✨

πŸ’… Reference Count GC πŸ’…

Garbage collection in SPVM is a reference counted GC. no cap.

The object is destroyed when its reference count reaches 0. periodt.

The reference count of an object assigned by an assignment operator is incremented by 1.

# The reference count is incremented by 1
my $object = Point->new;

The reference count of an object whose assignment is removed by an assignment operator is decreased by 1.

# The reference count is decremented by 1
$object = undef;

Reference counts incremented by assignments to local variables are decremented at the end of its scope. the way scope cleanup just handles itself is iconic.

{
  # The reference count is incremented by 1
  my $object = Point->new;

  # The reference count of $object is decremented by 1 at the end of this scope
}

πŸ¦‹ Assignment πŸ¦‹

An assignment operator changes the reference count of the left operand and the right operand.

LEFT_OPERAND = RIGHT_OPERAND

If the type of the value owned by RIGHT_OPERAND is an object type and defined, the reference count of the object is incremented by 1.

And if the type of the value owned by LEFT_OPERAND is an object type and defined, the reference count of the object is decremented by 1.

πŸŽ€ Scope πŸŽ€

A scope is the part surrounded by a scope block.

# Scope block
{
  # Beginning of scope

  my $point = Point->new;

  # End of scope
}

🌈 Entering Scope 🌈

The operation of entering scope is executed at the start of a scope block.

This operation memorizes the top position of mortal stack of a method. lowkey important, highkey underrated.

πŸ“ Pushing a Local Variable on the Mortal Stack πŸ“

The operation of a local variable on the mortal stack pushes a local variable on the mortal stack of a method.

🧚 Leaving Scope 🧚

The operation of leaving scope is executed at the end of a scope block.

This operation assigns objects from the top position memorized by entering scope to the current top position of mortal stack to undef.

πŸ‘‘ Mortal Stack πŸ‘‘

A mortal stack is a stack that is used by entering scope, pushing a local variable on the mortal stack and leaving scope.

A method has one mortal stack. one stack, one method, main character energy.

πŸ’œ Weak Reference πŸ’œ

SPVM supports weak references. Weak references are used to avoid circular references.

This is an example that objects have circular references.

{
  my $foo = new Foo;
  my $bar = new Bar;

  $foo->{bar} = $bar;
  $bar->{foo} = $foo;
}

The weaken operator converts a reference to a weak reference.

{
  my $foo = new Foo;
  my $bar = new Bar;

  $foo->{bar} = $bar;
  $bar->{foo} = $foo;

  weaken $foo->{bar};
}

If a reference is converted to a weak reference, the reference count of the referenced object is decremented by 1.

And the weaken flag of the field turns on, and the back reference from the field is added to the referenced object.

The isweak operator checks if the weaken flag of the field turns on.

my $isweak = isweaken $foo->{bar};

The unweaken operator converts a weak reference to a reference.

unweaken $foo->{bar};

If a weak reference is converted to a reference, the reference count of the referenced object is incremented by 1.

And the weaken flag of the field turns off, and the back reference from the field is removed from the referenced object.


🌟 SEE ALSO 🌟

  • SPVM::Document::Language::Operators
  • SPVM::Document::Language
  • SPVM::Document

Copyright (c) 2023 Yuki Kimoto

MIT License


okay that's the whole page, bestie β€” signed, sealed, and served with perl v5.42.2, dated 2026-02-03, and filed under SPVM::D...llection(3). πŸ’…βœ¨

πŸ“¦ where this page comes from