Skip to content

Core\Budget::memoryHeld

Core\Budget::memoryHeld(): uint

Returns the number of bytes of memory your program is using right now.

The number covers the values your program has made and still uses: its strings, arrays and objects. It goes down again as soon as a value is no longer used, so a program that has finished with a large file reads a small number again. You do not have to wait for the program to end.

Read it before a piece of work and again after it. The difference between the two readings is what that work costs. Compare it with Core\Budget::memoryLimit to see how much room is left.

Good to know: this is the memory of your program alone, not of the whole server. For the whole server, use Core\Os::residentBytes.

Return value

Returns uint. The number of bytes this request uses. It does not count the rest of the server process. Core\Os::residentBytes returns the memory of the whole process.

Examples

What a piece of work costs

<?nvs
// Shows how to measure the memory one piece of work uses. The program reads
// the figure before and after building a list. The difference between the two
// readings is what the list costs.
uint $before = Core\Budget::memoryHeld();
array<string> $names = [];
int $i = 0;
while ($i < 10000) {
$names[] = 'customer number ' . $i;
$i = $i + 1;
}
uint $after = Core\Budget::memoryHeld();
uint $used = $after - $before;
// The exact number of bytes changes between runs, so this program reports a
// size instead. This prints "the list uses more than 100 KB".
echo $used > 100 * 1024 ? "the list uses more than 100 KB\n" : "the list uses less than 100 KB\n";
Output
the list uses more than 100 KB

Memory is free again after use

<?nvs
// Shows that memory is free again as soon as your program stops using a value.
// Novis frees a value when nothing uses it any more, so the figure goes down
// during the program.
uint $start = Core\Budget::memoryHeld();
array<string> $lines = [];
int $i = 0;
while ($i < 20000) {
$lines[] = 'one line of a report';
$i = $i + 1;
}
uint $full = Core\Budget::memoryHeld();
// The report is written, so the program clears the list.
$lines = [];
uint $cleared = Core\Budget::memoryHeld();
// This prints "yes" twice. Building the list raised the figure, and clearing
// the list lowered it again.
echo $full > $start ? "yes\n" : "no\n";
echo $cleared < $full ? "yes\n" : "no\n";
Output
yes
yes

A batch job that watches itself

<?nvs
// Shows how a job that runs in batches watches its own memory. It reads the
// figure before each batch. It stops early when it has used half of the limit.
uint $limit = Core\Budget::memoryLimit();
array<string> $batch = [];
int $done = 0;
while ($done < 8) {
// A limit of 0 means there is no limit, so that program never stops early.
if ($limit > 0 && Core\Budget::memoryHeld() * 2 > $limit) {
break;
}
// Each batch replaces the one before it, so the memory of the last batch
// is free again.
$batch = [];
int $row = 0;
while ($row < 5000) {
$batch[] = 'invoice row ' . $row;
$row = $row + 1;
}
$done = $done + 1;
}
// Every batch is small, so all eight of them run. This prints 8.
echo $done, "\n";
Output
8