VOGONS


First post, by superfury

User metadata
Rank l33t++
Rank
l33t++

Do emulators have to do something when SDL_APP_LOWMEMORY is received? Since all allocations are used all the time? Flush all memory allocated to disk and reallocate on becoming active again(standby mode), otherwise terminate?

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 1 of 11, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie

Thats the OS giving you a hint that you should do something. This comes from Android/IOS when you pause or leave the app (but its still running) and you should release resources back to the OS.

Check the "README-IOS.TXT" for more explanation.

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 2 of 11, by superfury

User metadata
Rank l33t++
Rank
l33t++

So what CAN I do without losing any emulation data? Everything that's allocated is mostly RAM blocks containing emulation states(VGA VRAM, video rendering helper data allocated through SDL2, precalculation tables used for various hardware and emulated RAM memory containing all emulated stuff(so everything the PC uses to run, like emulated RAM, ROM etc.))

What stuff should be written to disk (and read back when becoming active again)?

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 3 of 11, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie

your app is a guest, so you should put everything you can on disk, etc, removed from ram. if the user hits the phone button and your app does nothing with all its memory then the user cant make a call. your app has zero importance compared to anything else.

if you loose emulation data, then you loose it.

read more up on android/ios development and memory handling, state and backgrounding.

you'd get better advice on a real mobile dev oriented forum than here.

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--

Reply 4 of 11, by Dominus

User metadata
Rank DOSBox Moderator
Rank
DOSBox Moderator

You should know yourself best what your emulator needs to get back on track.
Correct behavior of your emulator should be to dump some kind of savestate when entering background (there is an SDL event for that as well). Because you don't know when the user just closes the background app OR when the OS decides that your app is hogging too much memory (well you know when that event is triggered) and soon autocloses it.

Windows 3.1x guide for DOSBox
60 seconds guide to DOSBox
DOSBox SVN snapshot for macOS (10.4-11.x ppc/intel 32/64bit) notarized for gatekeeper

Reply 5 of 11, by superfury

User metadata
Rank l33t++
Rank
l33t++

IF I modify it to dump all dynamically allocated data to disk, can a foreground event(SDL_APP_SIDENTERFOREGROUND) be assured to reallocate all those? So up to 64MB(autodetected using malloc, I usually limit it to 4MB) RAM buffer as well as 1MB video memory, VGA VRAM(256k) and various hardware emulation buffers(up to 50000 16-bit samples for each channel, precalcs for audio resampling, video resizing precalcs(horizontal and vertical for each source SDL surface(wrapper around SDL_Surface object) and other small buffers)?

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 6 of 11, by Dominus

User metadata
Rank DOSBox Moderator
Rank
DOSBox Moderator

Again, this is up to you. All the OS is doing is reporting that you are back in foreground. Your app needs to make sure it reacts appropriately

Windows 3.1x guide for DOSBox
60 seconds guide to DOSBox
DOSBox SVN snapshot for macOS (10.4-11.x ppc/intel 32/64bit) notarized for gatekeeper

Reply 7 of 11, by superfury

User metadata
Rank l33t++
Rank
l33t++

Like I said, if I store that data(up to 100MB+) in files and free the memory. Then if I reallocate the memory using malloc during SDL_APP_DIDENTERFOREGROUND, will the reallocation(up to ~100MB+ previously freed) always succeed? Is it possible for the malloc(reallocating those buffers) to fail? If it can fail reallocating when restoring state(same memory the app originally used before the low memory event), it can and WILL crash, because all those buffers in their original size and data are required for the application to function without errors or memory shortage. ? If it can't reallocate, my app can and WILL fail because all allocated buffers contain data required for normal operation of the emulator and input/output.

It's 67.8MB according to Android's maintenance information. That's the basic XT with AweROMGM soundfont and VGA emulation with 1MB RAM autodetected(limited to 4MB(max 64MB) on AT+).

Last edited by superfury on 2017-11-28, 20:08. Edited 1 time in total.

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 8 of 11, by Dominus

User metadata
Rank DOSBox Moderator
Rank
DOSBox Moderator

Just try it or use the ever giving Stackoverflow to get better answers 😀
You may just need to make sure in your code that what you saved is all loaded back and if not gracefully exit.

Windows 3.1x guide for DOSBox
60 seconds guide to DOSBox
DOSBox SVN snapshot for macOS (10.4-11.x ppc/intel 32/64bit) notarized for gatekeeper

Reply 9 of 11, by vladstamate

User metadata
Rank Oldbie
Rank
Oldbie
superfury wrote:

Like I said, if I store that data(up to 100MB+) in files and free the memory. Then if I reallocate the memory using malloc during SDL_APP_DIDENTERFOREGROUND, will the reallocation(up to ~100MB+ previously freed) always succeed?

No. There is a big chance you will not be able to re-alocate. And yes, you will have to crash. Because another (misbehaved) application did not release its memory so the OS has not space for your allocations.

I know nothing about Android but in iOS there is no paging or virtual memory support. Whatever is available physically it is all there is. The memory will not grow to allow the foreground app to allocate whatever it needs. Which is why it is designed to send you messages like SDL_APP_LOWMEMORY. On mobile devices memory is much more of a physical barrier than on PCs due to lack of virtual management.

YouTube channel: https://www.youtube.com/channel/UC7HbC_nq8t1S9l7qGYL0mTA
Collection: http://www.digiloguemuseum.com/index.html
Emulator: https://sites.google.com/site/capex86/
Raytracer: https://sites.google.com/site/opaqueraytracer/

Reply 10 of 11, by superfury

User metadata
Rank l33t++
Rank
l33t++

So in other words: I'll have to redirect SDL_APP_LOWMEMORY to SDL_APP_TERMINATING, to let the main thread(and the other threads) tear the emulator down, save progress(BIOS Settings containing CMOS state) and quit the app?

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 11 of 11, by BloodyCactus

User metadata
Rank Oldbie
Rank
Oldbie

no, save everything and acknowledge the message. when it comes back, if you cant allocate memory, just pop up an error box saying not enough memory. you already have saved state, so if your app restarts clean later, it just reloads state. no big deal.

--/\-[ Stu : Bloody Cactus :: [ https://bloodycactus.com :: http://kråketær.com ]-/\--