Running a no limit query is very likely the cause of your memory issues. So you might want to change your query.
If still you need to do this way, you can use the fields
option, it returns post IDs only and will save a lot of memory usage – example below:
$your_query = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'post',
'fields' => 'ids',
)
);