# How FadCam Manages Memory and Battery Life During Continuous Recording

> Learn how FadCam optimizes memory and battery during continuous recording with WakeLocks, hardware acceleration, and disk caching for extended performance.

- Repository: [Faded/FadCam](https://github.com/anonfaded/FadCam)
- Tags: internals
- Published: 2026-05-13

---

**FadCam utilizes partial WakeLocks, hardware-accelerated MediaRecorder pipelines, and disk-based caching to maintain hours of continuous recording without exhausting RAM or causing excessive battery drain.**

The `anonfaded/FadCam` open-source Android application is designed specifically for long-duration video capture that can extend for multiple hours. To sustain this workload without triggering system kills or rapid battery depletion, the codebase implements a coordinated architecture of foreground service wake-locks, direct GPU-to-disk streaming, and aggressive temporary file management that minimizes memory and battery life impact during continuous recording.

## Battery Optimization Through WakeLocks

FadCam prevents the CPU from entering sleep states during active recording by running the camera operations inside dedicated foreground services that acquire partial wake locks.

### Foreground Service Architecture

Recording operations run inside specialized Android services including `RecordingService`, `ScreenRecordingService`, and `DualCameraRecordingService`. Each service registers as a foreground service with an ongoing notification, ensuring the Android system treats the process as high-priority and exempts it from standard background battery restrictions.

### Partial WakeLock Implementation

Within each service, the code acquires a **`PowerManager.PARTIAL_WAKE_LOCK`** immediately upon session start. This specific lock type keeps the CPU awake while allowing the screen to dim or turn off, which significantly reduces power consumption compared to full screen-on locks.

In [`app/src/main/java/com/fadcam/services/RecordingService.java`](https://github.com/anonfaded/FadCam/blob/main/app/src/main/java/com/fadcam/services/RecordingService.java), the implementation follows this pattern:

```java
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
recordingWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                   "FadCam:RecordingService");
recordingWakeLock.acquire();

```

The lock is held for the duration of the recording session and released explicitly in the service's lifecycle methods. According to the source code at lines 302–307 and 1734–1737, the release occurs in `onDestroy()` or when `stopRecording()` is invoked:

```java
private void releaseWakeLock() {
    if (recordingWakeLock != null && recordingWakeLock.isHeld()) {
        recordingWakeLock.release();
    }
}

```

The `ScreenRecordingService` and `DualCameraRecordingService` implement identical patterns with distinct lock tags (e.g., `"FadCam:ScreenRecordingWakeLock"`), ensuring that each recording modality maintains independent power state control.

## Efficient Media Recording Pipeline

Rather than buffering full video frames in application memory, FadCam streams data directly from the camera surface to the encoder, eliminating intermediate memory copies that would otherwise consume RAM proportionally to recording duration.

### Hardware-Accelerated Encoding

The application configures `MediaRecorder` with hardware-supported codecs and directs output to a file descriptor immediately. In [`app/src/main/java/com/fadcam/opengl/GLRecordingPipeline.java`](https://github.com/anonfaded/FadCam/blob/main/app/src/main/java/com/fadcam/opengl/GLRecordingPipeline.java) (lines 2675–2678), the recorder initializes with efficient H.264 encoding and directs audio from the microphone:

```java
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setVideoSize(width, height);
mediaRecorder.setVideoFrameRate(fps);
mediaRecorder.setOutputFile(outputPath);
mediaRecorder.prepare();
mediaRecorder.start();

```

### Direct Surface-to-Recorder Streaming

By leveraging OpenGL surfaces, frames render directly into the `MediaRecorder` input surface without passing through Java byte arrays. This pipeline, implemented in [`GLRecordingPipeline.java`](https://github.com/anonfaded/FadCam/blob/main/GLRecordingPipeline.java), ensures that RAM usage remains constant regardless of whether the recording lasts minutes or hours, as the encoded video bitstream writes directly to disk.

## Memory Management Strategies

FadCam treats memory as a scarce resource, offloading all heavy data to the filesystem and maintaining only lightweight metadata handles in heap memory.

### Temporary Thumbnail Caching

Video thumbnails are generated for UI previews but are never stored persistently in memory. Instead, [`app/src/main/java/com/fadcam/utils/VideoSessionCache.java`](https://github.com/anonfaded/FadCam/blob/main/app/src/main/java/com/fadcam/utils/VideoSessionCache.java) writes temporary PNG files to the application's cache directory (`context.getCacheDir()`), which the system can reclaim under memory pressure:

```java
File cacheFile = new File(context.getCacheDir(), "thumb_" + videoId + ".png");
try (FileOutputStream fos = new FileOutputStream(cacheFile)) {
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
}

```

The cache implementation automatically purges these files when sessions end or when size limits are exceeded, preventing unbounded disk growth and memory-mapped file accumulation.

### Deferred File Operations and Trash Management

Deletion operations utilize a soft-delete pattern to avoid I/O spikes that could interrupt recording. [`app/src/main/java/com/fadcam/utils/TrashManager.java`](https://github.com/anonfaded/FadCam/blob/main/app/src/main/java/com/fadcam/utils/TrashManager.java) moves unwanted videos to a dedicated trash directory within `context.getExternalFilesDir(null)` rather than unlinking them immediately. A lightweight JSON metadata file ([`trash_metadata.json`](https://github.com/anonfaded/FadCam/blob/main/trash_metadata.json)) stored in the internal files directory tracks these pending deletions, ensuring that heavy filesystem operations occur only when the device is idle or when the user explicitly empties the trash.

## Summary

- **Partial WakeLocks** keep the CPU alive while allowing the screen to sleep, minimizing battery drain during long recordings in [`RecordingService.java`](https://github.com/anonfaded/FadCam/blob/main/RecordingService.java) and related service classes.
- **Direct GPU-to-disk streaming** via [`GLRecordingPipeline.java`](https://github.com/anonfaded/FadCam/blob/main/GLRecordingPipeline.java) eliminates in-memory video buffering, keeping RAM usage flat regardless of recording length.
- **Hardware-accelerated MediaRecorder** configuration offloads encoding to dedicated DSPs rather than software threads, reducing CPU utilization and power consumption.
- **Cache directory offloading** for thumbnails and deferred deletion via [`TrashManager.java`](https://github.com/anonfaded/FadCam/blob/main/TrashManager.java) ensures that temporary data never accumulates in heap memory or primary storage permanently.

## Frequently Asked Questions

### Does FadCam keep the screen on during recording?

No. The application acquires a **`PARTIAL_WAKE_LOCK`** rather than a full screen wake lock. This allows the display to dim or turn off according to system settings while keeping the CPU awake enough to continue encoding video, which significantly reduces power consumption compared to forcing the screen to stay lit.

### How does FadCam prevent video data from filling up RAM?

Instead of accumulating video frames in byte arrays, FadCam feeds camera frames through an OpenGL surface directly into the `MediaRecorder` instance, which streams the encoded H.264 data straight to the output file on disk. This architecture, centered in [`GLRecordingPipeline.java`](https://github.com/anonfaded/FadCam/blob/main/GLRecordingPipeline.java), maintains constant memory usage that does not scale with recording duration.

### What happens to temporary files if the app crashes during recording?

Temporary thumbnail files stored in `context.getCacheDir()` are subject to the Android cache management system and may be cleared by the OS automatically upon restart. For video files themselves, the `MediaRecorder` writes to the specified output path incrementally, meaning a crash leaves a playable partial file up to the last sync frame rather than losing data held in memory.

### Why does FadCam use a partial WakeLock instead of a full one?

A partial wake lock keeps the CPU running without preventing the display from sleeping, which is essential for covert or battery-conscious recording scenarios. Full wake locks would force the screen to remain on, draining the battery rapidly and generating unnecessary heat during extended sessions. The partial lock provides sufficient resources for video encoding while respecting device power management policies.