Skip to content

Conversation

@ruchalzi
Copy link

@ruchalzi ruchalzi commented Apr 15, 2024

Memory Optimization for Batch Request Processing

Problem Statement
The previous implementation of batch request processing in Apache Olingo OData2 stored entire request bodies in memory as byte buffers. With large batch requests, this led to significant memory consumption and could potentially cause OutOfMemoryError in extreme cases.

Solution
Introduced a hybrid mechanism that dynamically switches between memory buffering and temporary files:

Small requests (< 64 KB): Remain in memory for optimal performance
Large requests (≥ 64 KB): Automatically spilled to temporary files
Key Changes

  1. New BatchInputResource Class
    Introduced a wrapper class for InputStream with size information, enabling unified processing of both in-memory and file-based data.

  2. Modified BatchHelper.BodyBuilder
    Activation threshold: 64 KB (8 × 8192 bytes)
    Automatic switching to temporary files when threshold is exceeded
    Configurable temporary directory via olingo.tmpdir system property

  3. Automatic Resource Cleanup
    The DeleteOnCloseFileInputStream class automatically deletes temporary files when the stream is closed, ensuring no leftover files in the filesystem.

  4. API Changes
    Replaced methods:

getBody() / getBodyAsBytes() → getBatchInputResource()
Benefits
✅ Dramatically reduced heap memory usage for large batch requests
✅ Eliminates risk of OutOfMemoryError with massive batch operations
✅ Performance preserved for small requests (remain in memory)
✅ Automatic lifecycle management of temporary files
✅ Backward compatibility - existing tests pass successfully

Technical Details
Temporary file location: Configurable via -Dolingo.tmpdir=/custom/path or defaults to java.io.tmpdir
Switching threshold: 65,536 bytes (8 × 8192)
File naming pattern: odata*.olingo in temporary directory
Modified Files
BatchInputResource.java - new class
DeleteOnCloseFileInputStream.java - new class
BatchChangeSetPart.java - API changes
BatchChangeSetPartImpl.java - new API implementation
BatchHelper.java - main hybrid buffering logic
BatchRequestWriter.java - usage of new API
Testing
All existing batch-related tests pass without modifications, confirming backward compatibility of the implementation.

ruchalzi added 2 commits March 6, 2024 17:40
creating a batchSet using files instead of buffers kept in memory aft…
@ruchalzi ruchalzi changed the title Reducing memory usage when using batch request (avoiding OOM even if huge data is transferred) Memory Optimization for Batch Request Processing Nov 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant