Skip to content

sys

ts
import {memoryUsage, uptime, gc, restart, exit, panic, getEnv} from 'mikrojs/sys'

System-level functions for memory monitoring, timing, garbage collection, and environment variables.

Functions

memoryUsage()

ts
function memoryUsage(): MemoryUsage

Returns current heap usage.

ts
const mem = memoryUsage()
const free = mem.heapTotal - mem.heapUsed
console.log('Free memory:', free / 1000, 'KB')

uptime()

ts
function uptime(): Uptime

Returns time since boot and RTC time.

ts
const {boot, rtc} = uptime()
console.log('Up for', boot, 'ms')
console.log('RTC:', rtc, 'ms')
PropertyDescription
bootMilliseconds since last boot (resets on deep sleep)
rtcMilliseconds from RTC clock (survives deep sleep)

gc()

ts
function gc(): void

Triggers garbage collection. Useful on memory-constrained devices to reclaim memory at a predictable point.

restart()

ts
function restart(): never

Restarts the device immediately.

exit(exitCode?)

ts
function exit(exitCode?: number): never

Stops the current program. On device, this returns to the REPL.

panic(message)

ts
function panic(message: string): never

Immediately crashes with an error message. Use for truly unrecoverable situations.

getEnv(varName)

ts
function getEnv(varName: string): string | undefined

Reads an environment variable. On device, environment variables are stored in NVS (non-volatile storage) and managed via mikro env.

ts
const apiKey = getEnv('API_KEY')

setSystemTime(timestamp)

ts
function setSystemTime(timestamp: number): void
function setSystemTime(date: Date): void

Sets the system clock. Typically called after SNTP sync or with a known timestamp.

Types

MemoryUsage

ts
interface MemoryUsage {
  heapUsed: number
  heapTotal: number
}

Uptime

ts
interface Uptime {
  readonly boot: number // ms since boot
  readonly rtc: number // ms from RTC
}

MonotonicTimestamp

A timestamp based on uptime that is not affected by NTP adjustments. Useful for measuring elapsed time reliably.

ts
const start = MonotonicTimestamp.now()
// ... do work ...
const elapsed = MonotonicTimestamp.now().since(start)
console.log('Took', elapsed, 'ms')
MethodDescription
MonotonicTimestamp.now()Create a timestamp at the current moment
.since(other)Milliseconds elapsed since another timestamp
.valueOf()Convert to wall-clock epoch ms
.toDate()Convert to a Date
.toISOString()ISO 8601 string