Generally, if you know your total memory, you can just manage it locally completely but if you don't, you can use this API to get current value:
https://api.apify.com/v2/users/me/limits?token=TOKEN. Just keep in mind that meanwhile some other task could have started if you don't have sole control over the account.