BlitzMax

BlitzMax

  • Downloads
  • Docs
  • API
  • Resources
  • About

›Advanced Topics

Setup

  • Getting Started
  • Win32
  • Linux
  • macOS
  • Android
  • iOS
  • Raspberry Pi
  • NX (Switch Homebrew)
  • Custom Settings

Tutorials

  • Beginners Guide
  • OOP Tutorial
  • Network Programming
  • TCP Socket Programming

Language

  • Arrays
  • Basic Compatibility
  • Collections
  • Comments
  • Conditional Compiling
  • Constants
  • Data Types
  • Debugging
  • Enums
  • Exceptions
  • Expressions
  • Functions
  • Identifiers
  • Literals
  • Modules
  • Objects
  • Program Flow
  • Slices
  • Strings
  • User Defined Types
  • Variables
  • Advanced Topics

    • Interfacing With C
    • Memory Management
    • Pointers
    • Custom Pre/Post Compilation Scripts
    • Creating DLLs

Tools

  • MaxIDE
  • BlitzMax Make (bmk)
  • BlitzMax Compiler (bcc)
Edit

Memory Management

BlitzMax currently uses an optimized form of reference counting to implement memory management.

However, there is one situation where you need to be a bit careful about memory management and that is when you are dealing with cyclic data structures.

A cyclic data structure is a data structure (in BlitzMax, a user defined type) that can potentially 'point to itself'. The most extreme example of a cyclic data structure is something like:

Type TCyclic
    Field cycle:TCyclic=Self
End Type

For k=1 To 10
    Local cyclic:TCyclic=New TCyclic
    GCCollect
    Print GCMemAlloced()
Next

If you run this program, you will notice that memory is slowly leaking. This is happening because the reference count for each TCyclic object never reaches 0, thanks to the objects 'cyclic' field.

It is therefore necessary to ensure such cyclic data structures are cleanly handled. This can be achieved by adding a method to 'unlink' any such cycles. For example:

Type TCyclic
    Field cycle:TCyclic=Self

    Method Remove()
        cycle=Null
    End Method

End Type

For k=1 To 10
    Local cyclic:TCyclic=New TCyclic
    GCCollect
    Print GCMemAlloced()
    cyclic.Remove
Next

In real world use this problem seldom arises, and when it does it's typically when designing container types.

If you are at all worried about this, you can easily track memory usage with the GCMemAlloced() function from the BlitzMax runtime library.

← Interfacing With CPointers →
BlitzMax
Docs
Getting StartedDownloadsAbout
Community
ResourcesSyntaxBomb Forums
More
GitHubStarChat on Discord
Copyright © 2022 Bruce A Henderson