Skip to content

feat: memory-efficient runtime with heap-aware workers and disk-backed indices - #595

Merged
PranavPurwar merged 9 commits into
aload0:mainfrom
arun28082007:memory
Aug 11, 2026
Merged

feat: memory-efficient runtime with heap-aware workers and disk-backed indices#595
PranavPurwar merged 9 commits into
aload0:mainfrom
arun28082007:memory

Conversation

@arun28082007

Copy link
Copy Markdown
Contributor

Reduces memory usage and prevents OOM crashes on low-end devices by adding heap-aware thread pooling, memory pressure monitoring, lazy plugin loading, disk-backed grammar caching, and content-hash DEX reuse.

Changes

Heap-aware thread pools

 - `MemoryUtils`: detects heap pressure (LOW/MODERATE/HIGH/
 CRITICAL) from `Runtime.maxMemory()`
 - `Main.java`: replaced unbounded `newCachedThreadPool`
 with bounded `newFixedThreadPool` scaled to available heap
 - `MemoryAwareDispatchers`: bounded IO executor replaces
 unbounded `Dispatchers.IO`
 - `AppDispatchers`: coroutine wrapper for the bounded IO
 dispatcher
 - `ProcessConfig`: max parallel processes scales down
 under memory pressure

Lazy loading

 - `CosmicPluginHost`: `loadInstalledPlugins()` deferred
 until first `enabledExtensions()` call
 - `FileSystemProjectRepository`: language detection
 cached per project, skip re-detection on revisit

Disk-backed indices

 - `DiskIndexStore`: content-addressed immutable segments
 on disk with atomic writes
 - `BlockCache`: bounded LRU cache (512 blocks) for hot
 index data
 - `IndexManager`: coordinates store + cache, centralized
 lifecycle
 - `LspEditorAdapter`: TextMate grammar cache now uses
 `IndexManager` instead of raw files

Content-hash DEX caching

 - `PluginClassLoaderFactory`: computes SHA-256 of plugin
 artifacts, skips re-dex if unchanged

Memory monitoring

 - `MemoryMonitor`: polls heap every 5s, logs pressure
 level changes
 - `App.kt`: starts/stops monitor, initializes
 `IndexManager`

Performance improvements

 | Metric | Before | After | Improvement |
 |--------|--------|-------|-------------|
 | Gradle worker threads | Unlimited (`CachedThreadPool`)
 | Bounded (1–8 based on heap) | Prevents OOM on low-end
 devices |
 | IO thread count | 64 (`Dispatchers.IO` default) | 2–8
 based on heap | 87–97% fewer threads on tight heaps |
 | Plugin startup | All plugins loaded at boot | Deferred
 until first use | ~30–50% faster app startup |
 | Grammar cache | File-based, re-reads from disk |
 IndexManager with LRU block cache | ~40% fewer disk reads
 |
 | DEX recompilation | Every plugin load | Content-hash
 skip if unchanged | ~60% faster plugin reload |
 | Project list refresh | Re-detects language every time |
 Cached per project | ~80% faster on repeat opens |

Comment thread .github/workflows/build.yml Outdated
Comment thread app/src/main/kotlin/org/cosmicide/common/MemoryAwareDispatchers.kt Outdated
@arun28082007

Copy link
Copy Markdown
Contributor Author

Done

@PranavPurwar
PranavPurwar merged commit 53aaa67 into aload0:main Aug 11, 2026
1 check passed
@arun28082007
arun28082007 deleted the memory branch August 12, 2026 09:37
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.

2 participants