androidx / androidx/media

OutOfMemory in SimpleCache.loadDirectory

Open
#2,303 4 comments 0 reactions 1 assignee View on GitHub

@tianyif is already working on this.

Since Apr 3, 2025.

needs triage question
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

Version

Media3 main branch

More version details

No response

Devices that reproduce the issue

  • Samsung running Android 13 (RAM free: 870.45 MB Disk free: 12.37 GB)
  • Oppo running Android 14 (RAM free: 1.96 GB Disk free: 13.85 GB)

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

No

Reproduction steps

Not able to reproduce as this is reported via crashlytics.

Expected result

Should not get OOM

Actual result

Getting OOM exception

Issue

Need help to fix this or any suggestion which can help here to fix SimpleCache outofmemory issue.

I also tried the LeastRecentlyUsedCacheEvictor but by that way offline videos were not getting played when maxBytes gets exceeded.

I am not able to reproduce this issue in my device, got this issue from crashlytics.

Attaching the stack trace for the crash below:

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 120 byte allocation with 1468512 free bytes and 1434KB until OOM, target footprint 536870912, growth limit 536870912; giving up on allocation because <1% of heap free after GC.
 at java.io.UnixFileSystem.resolve(UnixFileSystem.java:146)
 at java.io.File.<init>(File.java:273)
 at java.io.File.listFiles(File.java:1235)
 at com.google.android.exoplayer2.upstream.cache.SimpleCache.loadDirectory(SimpleCache.java:633)
 at com.google.android.exoplayer2.upstream.cache.SimpleCache.initialize(SimpleCache.java:586)
 at com.google.android.exoplayer2.upstream.cache.SimpleCache.access$000(SimpleCache.java:49)
 at com.google.android.exoplayer2.upstream.cache.SimpleCache$1.run(SimpleCache.java:268)

Attaching the code for creating simple cache object:

val cookieManager = CookieManager()
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER)
CookieHandler.setDefault(cookieManager)
val standaloneDatabaseProvider = StandaloneDatabaseProvider(context)
val downloadContentDirectory = File(context.filesDir.absolutePath + "/" + APP_NAME + "/Videos")
val simpleCache = SimpleCache(
    downloadContentDirectory, NoOpCacheEvictor(), standaloneDatabaseProvider
)
val upstreamFactory =
    DefaultDataSource.Factory(context, dataSourceFactory)
cacheDataStoreFactory = CacheDataSource.Factory()
    .setCache(simpleCache)
    .setUpstreamDataSourceFactory(upstreamFactory)
    .setCacheWriteDataSinkFactory(null)
    .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
downloadManager = DownloadManager(
    context,
    standaloneDatabaseProvider,
    simpleCache,
    ResolvingDataSource.Factory(dataSourceFactory, resolver),
    Executors.newFixedThreadPool(12) 
)

Bug Report

  • You will email the zip file produced by adb bugreport to android-media-github@google.com after filing this issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.