ZetCode

Dart ProcessInfo

last modified April 4, 2025

The ProcessInfo class in Dart provides information about the current process. It's part of the dart:io library and offers details like memory usage, process ID, and CPU time.

ProcessInfo is useful for monitoring, debugging, and performance analysis. It helps understand resource consumption and process characteristics in Dart apps.

Basic Definition

ProcessInfo is a utility class that exposes process-related metrics. It provides static methods to access information about the running Dart process.

Key features include memory usage statistics, process identification, and CPU time measurements. It works on all platforms supported by Dart's IO library.

Getting Process ID

This example shows how to retrieve the current process ID using ProcessInfo.

main.dart
import 'dart:io';

void main() {
  var pid = ProcessInfo.currentPid;
  print('Process ID: $pid');
}

We use the static currentPid property to get the process identifier. This is useful for logging and process management in multi-process applications.

$ dart main.dart
Process ID: 12345

Memory Usage Statistics

This example demonstrates how to check memory usage of the current process.

main.dart
import 'dart:io';

void main() async {
  var memory = await ProcessInfo.getCurrentRss();
  print('Resident Set Size: ${memory ~/ 1024} KB');
  
  var maxRss = await ProcessInfo.getMaxRss();
  print('Maximum RSS: ${maxRss ~/ 1024} KB');
}

We use getCurrentRss for current memory usage and getMaxRss for peak usage. These values help monitor memory consumption patterns in apps.

$ dart main.dart
Resident Set Size: 24576 KB
Maximum RSS: 25600 KB

CPU Time Measurement

This example shows how to measure CPU time consumed by the process.

main.dart
import 'dart:io';

void main() async {
  var start = await ProcessInfo.getCpuUsage();
  
  // Simulate CPU-intensive work
  var sum = 0;
  for (var i = 0; i < 100000000; i++) {
    sum += i;
  }
  
  var end = await ProcessInfo.getCpuUsage();
  print('CPU time used: ${end - start} microseconds');
}

We measure CPU time before and after a computation. The difference shows actual CPU time spent, useful for performance profiling and optimization.

$ dart main.dart
CPU time used: 125000 microseconds

System Information

This example demonstrates getting system-level process information.

main.dart
import 'dart:io';

void main() {
  print('Number of processors: ${ProcessInfo.numberOfProcessors}');
  print('Process start time: ${ProcessInfo.startTime}');
  print('System page size: ${ProcessInfo.pageSize} bytes');
}

We access system properties like CPU count and memory page size. These help optimize resource usage and understand the execution environment.

$ dart main.dart
Number of processors: 8
Process start time: 2025-04-04 10:30:45.123456Z
System page size: 4096 bytes

Monitoring Memory Growth

This example shows how to monitor memory growth over time.

main.dart
import 'dart:io';
import 'dart:async';

void main() async {
  var timer = Timer.periodic(Duration(seconds: 1), (timer) async {
    var rss = await ProcessInfo.getCurrentRss();
    print('Current RSS: ${rss ~/ 1024} KB');
  });
  
  // Allocate memory periodically
  var lists = <List<int>>[];
  Timer.periodic(Duration(seconds: 2), (timer) {
    lists.add(List.filled(1000000, 0));
  });
  
  await Future.delayed(Duration(seconds: 10));
  timer.cancel();
  print('Monitoring completed');
}

We set up periodic memory monitoring while artificially increasing memory usage. This pattern helps detect memory leaks and understand allocation patterns.

$ dart main.dart
Current RSS: 16384 KB
Current RSS: 16384 KB
Current RSS: 24576 KB
Current RSS: 24576 KB
Current RSS: 32768 KB
Monitoring completed

Best Practices

Source

Dart ProcessInfo Documentation

This tutorial covered Dart's ProcessInfo class with practical examples showing process monitoring, performance measurement, and system information access.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.