Memory Optimization for Batch Request Processing #52
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
New BatchInputResource Class
Introduced a wrapper class for InputStream with size information, enabling unified processing of both in-memory and file-based data.
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
Automatic Resource Cleanup
The DeleteOnCloseFileInputStream class automatically deletes temporary files when the stream is closed, ensuring no leftover files in the filesystem.
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.