Urgent.News

What's breaking now, across thousands of outlets.

Tech

Loading a 72 MB JSON list on a TV box without running out of memory

I make a media player for Android TV boxes, phones and Windows. People sign in with the playlist their provider gives them, and the app downloads three lists from the provider's server: live channels, films and series. Each one is a single JSON array. On paper that is easy: await (await fetch(url)).json() . In practice it broke on real hardware, and the fix taught me more about memory on cheap…

A media player for Android TV boxes, phones and Windows was created by the author. Users sign in with a playlist provided by their service, and the app downloads three lists from the provider's server: live channels, films and series. Each list is a single JSON array that can be large, such as 72 MB for one list or 131 MB when combining three lists.

The author discovered that downloading and parsing such large lists directly on an Android TV box with 4 GB of RAM could cause memory issues, leading to crashes and empty playlists.

To address this problem, the author implemented a fix where the JSON list is streamed instead of being downloaded as a whole. This stream reads the list as the bytes arrive, piece by piece, which prevents the entire list from being held in memory at once. The process then cuts the array and parses each item separately, reducing the number of large parses and allowing for better memory management.

The splitter, which handles the array parsing, reads only 2,000 items at a time and hands these items to JSON.parse on their own.

Another optimization the author made was to save the rows in fixed blocks, using a single prepared INSERT statement that can be reused. This prevents memory allocation from growing as the list is parsed. Additionally, the author addressed issues with backslashes and non-ASCII characters, which could cause performance issues when parsing the JSON. By handling these characters correctly, the parsing time became consistent regardless of the specific content within the list.

For Android devices, the author further optimized the process by first saving the list as a file in the cache folder using native code. The list is then read back in megabyte chunks, which keeps the memory usage low and improves performance compared to reading the entire list at once. If the device has less than 300 MB of free memory or cannot write the file, the app falls back to reading the list as it arrives.

The author recommends testing on the target device, treating memory issues as possible causes for "Network request failed," and avoiding using response.json() on large JSON lists. Batching data into fixed sizes and testing with real-world data containing escaped slashes and non-ASCII characters are advised for better performance in React Native apps. The techniques discussed can be applied to any application that requires loading large JSON data on devices with limited memory.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Read-only OAuth is one boundary. Your agent still needs others.

By Xplormity Collective A coding agent asks for Search Console data. You approve a read-only Google scope. Can it change your website? The answer depends on which tool it uses next.

  • Read-only OAuth allows data access without modification
  • SearchDock client demonstrates read-only implementation
  • Agent's capabilities extend beyond read-only actions

More from Saturday 14 November →