When does global/main code of plug execute?

Plugins are only loaded once per request. Plugins are loaded with this code:


// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
    wp_register_plugin_realpath( $plugin );
    include_once( $plugin );
    /**
     * Fires once a single activated plugin has loaded.
     *
     * @since 5.1.0
     *
     * @param string $plugin Full path to the plugin's main file.
     */
    do_action( 'plugin_loaded', $plugin );
}

This is inside wp-settings.php, which only loads once, but also note that include_once is used, meaning that the plugin couldn’t be loaded twice even if that code ran twice for some reason.

However, not that I said per request. This means that any AJAX requests that are sent from the page will cause the plugin to be loaded for each of those AJAX requests, in the background. This could explain why the function is running multiple times