sys
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()
function memoryUsage(): MemoryUsageReturns current heap usage.
const mem = memoryUsage()
const free = mem.heapTotal - mem.heapUsed
console.log('Free memory:', free / 1000, 'KB')uptime()
function uptime(): UptimeReturns time since boot and RTC time.
const {boot, rtc} = uptime()
console.log('Up for', boot, 'ms')
console.log('RTC:', rtc, 'ms')| Property | Description |
|---|---|
boot | Milliseconds since last boot (resets on deep sleep) |
rtc | Milliseconds from RTC clock (survives deep sleep) |
gc()
function gc(): voidTriggers garbage collection. Useful on memory-constrained devices to reclaim memory at a predictable point.
restart()
function restart(): neverRestarts the device immediately.
exit(exitCode?)
function exit(exitCode?: number): neverStops the current program. On device, this returns to the REPL.
panic(message)
function panic(message: string): neverImmediately crashes with an error message. Use for truly unrecoverable situations.
getEnv(varName)
function getEnv(varName: string): string | undefinedReads an environment variable. On device, environment variables are stored in NVS (non-volatile storage) and managed via mikro env.
const apiKey = getEnv('API_KEY')setSystemTime(timestamp)
function setSystemTime(timestamp: number): void
function setSystemTime(date: Date): voidSets the system clock. Typically called after SNTP sync or with a known timestamp.
Types
MemoryUsage
interface MemoryUsage {
heapUsed: number
heapTotal: number
}Uptime
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.
const start = MonotonicTimestamp.now()
// ... do work ...
const elapsed = MonotonicTimestamp.now().since(start)
console.log('Took', elapsed, 'ms')| Method | Description |
|---|---|
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 |