with profiler.profile_context("data_loading"): # Load large dataset data = load_dataset('large_file.csv') data = preprocess(data) features = extract_features(data)# Get profiling resultsfor result in profiler.results: print(f"Operation: {result.name}") print(f"RSS before: {result.snapshot_before.rss / (1024**2):.2f} MB") print(f"RSS after: {result.snapshot_after.rss / (1024**2):.2f} MB") print(f"CPU percent: {result.snapshot_after.cpu_percent:.1f}%")
4
Real-time monitoring
Monitor CPU memory over time:
# Start monitoringprofiler.start_monitoring(interval=0.5) # 500ms intervals# Your CPU-intensive codefor epoch in range(10): for batch in data_batches: process_batch(batch)# Stop and get summaryprofiler.stop_monitoring()summary = profiler.get_summary()print(f"Snapshots collected: {summary['snapshots_collected']}")print(f"Peak memory: {summary['peak_memory_usage'] / (1024**2):.2f} MB")print(f"Memory change: {summary['memory_change_from_baseline'] / (1024**2):.2f} MB")# Review snapshotsfor snapshot in profiler.snapshots[-10:]: print(f"Time: {snapshot.timestamp:.2f}, RSS: {snapshot.rss / (1024**2):.2f} MB")
The CLI automatically uses CPU mode when GPU is unavailable:
# CPU infogpumemprof info# Output:# CUDA is not available. Falling back to CPU-only profiling.# Process RSS: 512.00 MB# Process VMS: 2.50 GB# CPU Count: 8 physical / 16 logical# CPU monitoringgpumemprof monitor --duration 30 --interval 0.5 --output cpu_monitor.csv# Mode: CPU# CPU trackinggpumemprof track --duration 60 --output cpu_track.json --format json# Running CPU memory tracker (no GPU backend available).
import torchfrom gpumemprof import CPUMemoryProfilerprofiler = CPUMemoryProfiler()with profiler.profile_context("cpu_tensor_operations"): # Large CPU tensor operations x = torch.randn(10000, 10000) y = torch.randn(10000, 10000) result = x @ y result = result.sum()summary = profiler.get_summary()print(f"Peak memory: {summary['peak_memory_usage'] / (1024**2):.2f} MB")
import tensorflow as tffrom gpumemprof import CPUMemoryTrackertf.config.set_visible_devices([], 'GPU') # Force CPUtracker = CPUMemoryTracker(sampling_interval=0.5)tracker.start_tracking()# CPU-only TensorFlow operationswith tf.device('/CPU:0'): x = tf.random.normal([5000, 5000]) y = tf.random.normal([5000, 5000]) result = tf.matmul(x, y)tracker.stop_tracking()stats = tracker.get_statistics()print(f"Peak memory: {stats['peak_memory'] / (1024**2):.2f} MB")
import numpy as npimport pandas as pdfrom gpumemprof import CPUMemoryProfilerprofiler = CPUMemoryProfiler()# Profile data loadingwith profiler.profile_context("data_loading"): df = pd.read_csv('large_dataset.csv')# Profile preprocessingwith profiler.profile_context("preprocessing"): df = df.dropna() df['normalized'] = (df['value'] - df['value'].mean()) / df['value'].std() features = df.values# Profile feature extractionwith profiler.profile_context("feature_extraction"): features = np.concatenate([features, features**2], axis=1)# Review resultsfor result in profiler.results: print(f"{result.name}: {result.memory_diff() / (1024**2):.2f} MB")