Exhausted memory limit with very simple WP_Query

Your query looks OK, so I doubt that it will cause any problems. But then… Let’s take a look at your loop:

if ($mainPosts->have_posts()) {

    while ($mainPosts->have_posts()) {
        $postItemImage = the_post_thumbnail_url();
        ...    
        echo "...";

    }
}

First of all you don’t have any else in there, so there is no point in this if. But there is even bigger problem with this loop…

It will be an infinite loop, because there is no $mainPosts->the_post(), so it doesn’t “consume” posts, so there are always all of them when checking loop condition.

This will work fine:

while ($mainPosts->have_posts()) {
    $mainPosts->the_post();
    $postItemImage = the_post_thumbnail_url();
    ...    
    echo "...";
}