<!--
title: Enforce resource usage limits
domain: ai-agents
topic: Performance Optimization
language: TypeScript
source: RooCodeInc/Roo-Code
updated: 2025-07-16
url: https://awesomereviewers.com/reviewers/roo-code-enforce-resource-usage-limits/
-->

Implement explicit resource limits and monitoring to prevent performance degradation and memory issues. This includes:

1. Define maximum sizes for buffers and caches
2. Implement early size checking
3. Reuse buffers for large operations
4. Add monitoring/warnings when approaching limits

Example implementation:
```typescript
class ResourceAwareProcessor {
  private static readonly MAX_BUFFER_SIZE = 1024 * 1024; // 1MB
  private static readonly MAX_CACHE_ENTRIES = 1000;
  private buffer: Buffer | null = null;

  async processData(data: string): Promise<void> {
    // Early size check
    if (Buffer.byteLength(data) > this.MAX_BUFFER_SIZE) {
      throw new Error(`Data exceeds maximum size of ${this.MAX_BUFFER_SIZE} bytes`);
    }

    // Reuse buffer if possible
    if (!this.buffer) {
      this.buffer = Buffer.alloc(this.MAX_BUFFER_SIZE);
    }

    // Process with size awareness
    try {
      // ... processing logic
    } catch (error) {
      console.warn(`Processing warning: ${error.message}`);
    }
  }

  clearBuffer(): void {
    this.buffer = null;
  }
}
```

This approach:
- Prevents memory exhaustion
- Improves performance through buffer reuse
- Provides early failure for oversized inputs
- Enables monitoring and debugging
