Memory
- Dart course
- Advanced
- Lesson 75/83
- 5 min
Learn memory usage.
Memory usage
In Dart, memory usage is given by the ProcessInfo class in the dart:io library. ProcessInfo.currentRss gives the current resident set size (RSS) in bytes; ProcessInfo.maxRss gives the peak value reached during the run.
RSS is the amount of memory the process currently holds in RAM. The garbage collector manages memory automatically; it collects unused objects. Memory usage changes during a run, so the RSS value differs every run. For performance analysis you track RSS over time; a single measurement is not conclusive.
The garbage collector
Dart's garbage collector (GC) automatically finds unused objects and reclaims memory. The developer does no manual memory management like malloc/free. The GC scans the reachability graph of objects; objects not reachable from a root are marked as garbage.
For memory optimization: do not keep large lists needlessly, use final for stable references, and call cancel() on a StreamSubscription when the stream is done. This prevents memory leaks. The GC runs at regular intervals; heavy object creation increases GC pressure and can reduce performance.
Practice Memory API info
Add import 'dart:io';. In main add print('Memory API: ProcessInfo.currentRss') and print('Peak API: ProcessInfo.maxRss'). Since the RSS value differs every run, we print the API name instead of the real value.
Starter code
// import the dart:io library
void main() {
// print memory API info
}Sample solution
import 'dart:io';
void main() {
print('Memory API: ProcessInfo.currentRss');
print('Peak API: ProcessInfo.maxRss');
}Expected output
Memory API: ProcessInfo.currentRss Peak API: ProcessInfo.maxRss
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the memory parts
| Term | Meaning |
|---|---|
ProcessInfo |
Memory API |
currentRss |
Current RSS |
maxRss |
Peak value |
dart:io |
Library |
2 What does currentRss give?
- Time
- CPU percent
- Current RSS Correct answer
- Disk space
Explanation: currentRss gives the current memory (RSS) amount.
3 The ProcessInfo call
print('Memory: ${____.currentRss}');
print('Memory: ${ProcessInfo.currentRss}');
Explanation: ProcessInfo.currentRss gives the current RSS.