Memory Management when developing plug-ins

You could try to tackle this problem in blocks of posts instead of all of them at the same time, you are currently loading all the posts into the memory and then you query the database using these posts which isn’t terribly efficient.

You could get the total amount of post, and then query them in blocks of 25 or something each. Then do your queries and start over again with the next 25.

If you need I could whip up some code to illustrate my answer.

EDIT:
I have created an example which hopefully solves your problem. I fixed the first block of queries so if this works you (or I) can apply it to the second block in the same manner.

Unfortunately I don’t have a WordPress install handy so there might be bugs. Also I´m not entirely sure if wp_count_posts and $avail_posttypes play nice together.

<?php 
$internal   = array( 'page', 'post' );
$cpt_args = array(
    'public' => true,
    '_builtin' => false
);
$custom = get_post_types( $cpt_args, 'objects' ); 

foreach ($custom as $name => $data) {
    $internal[] = $name;
}
$avail_posttypes = $internal;
// Changed the numberposts to 25 to only query 25 posts, and added an offset.
$args = array(
    'post_type' => $avail_posttypes,
    'post_status'   => 'any',
    'numberposts'   => 25,
    'offset'   => 0,
    'cache_results' => false,
    'no_found_rows' => true,
    'fields'    => 'ids',
);


$total_amount_of_posts = wp_count_posts($avail_posttypes);

// Check if there are more post waiting if so then query them.
while(args['offset']+args['numberposts']<$total_amount_of_posts)
{
    // Get the next 25 posts.
    $posts = get_posts($args);

foreach ($posts as $post) {
    if( !update_post_meta( $post->ID, '_cf_one', 'ok', true ) ){
        add_post_meta( $post->ID, '_cf_one', 'ok', true );
    }
    if( !update_post_meta( $post->ID, '_cf_two', 'ok', true ) ){
        add_post_meta( $post->ID, '_cf_two', 'ok', true );
    }
    if( !update_post_meta( $post->ID, '_cf_three', 'ok', true ) ){
        add_post_meta( $post->ID, '_cf_three', 'ok', true );
    }
    if( !update_post_meta( $post->ID, '_cf_four', 'ok', true ) ){
        add_post_meta( $post->ID, '_cf_four', 'ok', true );
    }
}
// Skip over the 25 we just queried.
args['offset'] += 25;

}