[Android] Check memory size before setting the ringtone to avoid OOM

Nicolas Pomepuy git at videolan.org
Thu Sep 7 08:50:05 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Sep  6 08:14:53 2023 +0200| [1315e7eb720ec6ae7ad07cd4a01c26e6754bab01] | committer: Nicolas Pomepuy

Check memory size before setting the ringtone to avoid OOM

> https://code.videolan.org/videolan/vlc-android/commit/1315e7eb720ec6ae7ad07cd4a01c26e6754bab01
---

 application/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.kt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.kt
index 43900b2876..bd6500abfc 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/AudioUtil.kt
@@ -85,7 +85,10 @@ object AudioUtil {
                     val newUri: Uri = this.contentResolver
                             .insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values)!!
                     contentResolver.openOutputStream(newUri).use { os ->
+                        val runtime = Runtime.getRuntime()
+                        val availHeapSizeInMB = runtime.maxMemory() - runtime.totalMemory() - runtime.freeMemory()
                         val size = newRingtone.length().toInt()
+                        if (size > availHeapSizeInMB * 0.8F) throw IllegalStateException("Not enough memory")
                         val bytes = ByteArray(size)
                         val buf = BufferedInputStream(FileInputStream(newRingtone))
                         buf.read(bytes, 0, bytes.size)



More information about the Android mailing list