Mem
import "std/core" (included in import "std") · namespace std.core
How much memory a value uses at run time. (sizeof(x) is the size of the variable itself, fixed when compiling: 8 for any object, which a variable holds by reference.)
grav
import "std"
class Line {
public sku: string = ""
constructor(s: string) { self.sku = s }
}
fn main() {
let nums = new Array<long>()
for (i in 0..1000) { nums.push(i as long) }
let d = Mem.describe(nums)
print("${d.type}, ${d.items} items, ${d.objects} object")
print(Mem.sizeOf(nums) >= 8000)
let lines = new Array<Line>()
for (i in 0..10) { lines.push(new Line("SKU-${i}")) }
print(Mem.sizeOf(lines) < Mem.deepSizeOf(lines)) // the lines and their strings too
print(Mem.describe(lines).objects) // the array and 10 lines
print(Mem.sizeOf("a literal")) // in the program, owned by nobody
print(Mem.human(1536))
}Array<long>, 1000 items, 1 object
true
true
11
0
1.5 KBMem.sizeOf(x): the value itself. For an object, its allocation plus the buffers and strings it owns (an array's element buffer and the strings in it, a map's slots), but not the other objects it refers to. For a string, its bytes. For a number or boolean, 8.Mem.deepSizeOf(x):xand everything it refers to, following every reference, with each object and string counted once (shared and cyclic parts included).Mem.describe(x): aMemInfowith the readabletype,items(for an array, map or set; -1 otherwise),bytes,deepBytesand the number ofobjects; printed, it readsArray<long>: 1000000 items, 8.0 MB itself, 8.0 MB with what it holds (1 object).- What is counted. Sizes are what the memory allocator handed out, so a little more than asked for, and a growing array's spare capacity is included. Literal strings are not counted: they are part of the program. Memory a class keeps in C pointers of its own is counted when the class says so (a private
__bytesExtra() -> longmethod, as the std containers have).
API
class MemInfo
What Mem.describe found.
Constructors
new MemInfo()
Fields
type: string = ""— The value's type ("Array<long>", "app.Order", "string", "long", "null").items: long = -1— The elements of an array, map or set; -1 for anything else.bytes: long = 0— The value itself: an object's memory and the buffers and strings it owns.deepBytes: long = 0— With everything it refers to, each object and string counted once.objects: long = 0— The objects counted in deepBytes.
Methods
fn toString() -> string
class Mem
Static members
static fn sizeOf(x: any) -> long— The memoryxitself uses: an object's allocation plus the buffers and strings it owns (not the other objects it refers to); a string's bytes; 8 for a number or boolean.static fn deepSizeOf(x: any) -> long— The memoryxand everything it refers to use, each object and string counted once (shared or cyclic parts included).static fn describe(x: any) -> MemInfostatic fn stringBytes(s: string) -> long— A string's allocation (0 for a literal).static fn readable(t: string) -> string— "std.collections.Array$long" -> "Array<long>"static fn human(n: long) -> string— Bytes as text: "512 B", "1.5 KB", "8.0 MB", "1.2 GB".