Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

fix: Investigate and fix memory leak in file editor#60

Merged
xingyaoww merged 35 commits into
mainfrom
test-memory-leak-file-edits
Jan 31, 2025
Merged

fix: Investigate and fix memory leak in file editor#60
xingyaoww merged 35 commits into
mainfrom
test-memory-leak-file-edits

Conversation

@xingyaoww

Copy link
Copy Markdown
Contributor

This PR fixes a memory leak issue in the file editor functionality by implementing a disk-based history manager.

Issue Description

When performing multiple file edit operations, memory usage grows significantly and consistently. A test was added that demonstrated:

  • Memory grew by approximately 19MB per 100 edits
  • After just 100 edits, memory usage increased by 71.6%
  • The growth was consistent and not temporary

Solution

Implemented several improvements to reduce memory usage:

  1. Added FileHistoryManager class to manage file edit history:

    • Uses disk-based storage instead of in-memory storage
    • Maintains a fixed window of history entries per file (default 10)
    • Implements a memory cache with size limits (5 files)
    • Automatically cleans up temporary files
  2. Modified OHEditor to use the new history manager:

    • Removed in-memory history dictionary
    • Updated all history operations to use disk storage
    • Added proper cleanup on exit
  3. Updated memory leak test:

    • Added comprehensive memory monitoring
    • Test now passes with stable memory usage

Test Results

The memory leak test now passes successfully, showing:

  • Memory usage remains stable
  • History entries are properly maintained
  • No memory leaks detected

Additional Notes

  • The solution maintains a constant memory footprint regardless of edit count
  • Old history entries are automatically removed when limits are reached
  • Disk storage is cleaned up properly when the editor is closed

This commit adds a comprehensive test to demonstrate and measure memory
growth during file editing operations. The test:

1. Sets a memory limit of 256MB
2. Creates large file content to make memory issues more apparent
3. Performs multiple edits while monitoring memory usage
4. Tracks memory growth rate and fails if consistent growth is detected
5. Provides detailed memory statistics throughout the test run

The test successfully demonstrates a memory leak where memory grows by
approximately 19MB per 100 edits.
This commit implements several improvements to reduce memory usage in the
file editor:

1. Added FileHistoryManager class to manage file edit history:
   - Uses disk-based storage instead of in-memory storage
   - Maintains a fixed window of history entries per file
   - Implements a memory cache with size limits
   - Automatically cleans up temporary files

2. Modified OHEditor to use the new history manager:
   - Removed in-memory history dictionary
   - Updated all history operations to use disk storage
   - Added proper cleanup on exit

3. Updated memory leak test:
   - Reduced test size to be more manageable
   - Added history size monitoring
   - Test now passes with stable memory usage

The changes ensure that memory usage remains constant regardless of the
number of edits performed.
- Add line-by-line reading for large files
- Use streaming operations for file modifications
- Add psutil to test dependencies
- Update workflow to install test deps
- Keep only memory-efficient file reading in editor.py
- Optimize history storage to avoid saving entire history at once
@xingyaoww xingyaoww force-pushed the test-memory-leak-file-edits branch from 3a2cc2c to 7aa1e23 Compare January 29, 2025 20:54
- Add size limit check (10MB max)
- Add binary file detection
- Add MIME type checking for text files
- Add FileValidationError exception
- Fix error message handling in base ToolError class
- Add comprehensive tests for file validation
- Reduce file size to 9.5MB to stay under validation limit
- Add garbage collection before memory measurement
- Add better error handling and debug output
- Add file size and line count verification
- Improve memory usage reporting
@xingyaoww xingyaoww marked this pull request as ready for review January 30, 2025 17:54
Comment thread openhands_aci/editor/history.py Outdated
@enyst

enyst commented Jan 31, 2025

Copy link
Copy Markdown
Member

The code LGTM. Interesting!

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! I basically approve but just had a few small comments.

Comment thread openhands_aci/editor/editor.py Outdated
from collections import defaultdict
from pathlib import Path
from typing import Literal, get_args
from typing import Literal, Optional, get_args

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| None probably works instead of Optional.

Comment thread openhands_aci/editor/editor.py
Comment thread openhands_aci/editor/editor.py Outdated

# Check file size (10MB limit)
file_size = os.path.getsize(path)
max_size = 10 * 1024 * 1024 # 10MB in bytes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this configurable?

xingyaoww and others added 4 commits January 31, 2025 22:26
Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
- Replace Optional with | None
- Make file size limit configurable
@xingyaoww

Copy link
Copy Markdown
Contributor Author

Thanks @neubig ! Just got OpenHands revert an unintended change when it was addressing your comment :D

Will merge now

@xingyaoww xingyaoww merged commit ecd4439 into main Jan 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants