linux下写的c程序连接mysql , 内存占用很大100多m, 这咋回事mysql_free_result  mysql_close  都有的。。 但是一开开 ,内存总是这么大

解决方案 »

  1.   

    在linux环境  
    mysql_free_result
    mysql_close
    都用了,内存也没涨 但是太大了 始终100多M
      

  2.   

    #include <stdio.h>
    #include <pthread.h>
    #include <mysql/mysql.h>
    void *test1( void *arg )
    {
    char sql[1024];
    MYSQL_RES *results;
    MYSQL_ROW record;
    unsigned long id = 0;
    MYSQL *mysql;
    int mysql_timeout = 10;

    mysql = mysql_init( NULL ); mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout ); if ( !mysql_real_connect( mysql, "localhost", "root", "123456","db", 0, NULL, 0 ) )
    {
    printf("error");
    exit(1);
    }

    while( 1 )
    {
    snprintf( sql, 1024, "select * from test limit %ld,10", id ++ );

    if ( mysql_query( mysql, sql ) != 0 )
    {
    printf( "1 mysql:%s\n", mysql_error( mysql ) );
    }

    results = mysql_store_result( mysql ); if ( results == NULL )
    {
    printf( "2 mysql:%s\n", mysql_error( mysql ) );
    } while ( (record = mysql_fetch_row( results ) ) )
    {
    printf("%s - %s\n", record[0], record[1] );
    } if ( mysql_num_rows( results ) == 0 )
    {
    id = 0;
    }

    mysql_free_result( results );
    } return arg;
    }void *test2( void *arg )
    {
    char sql[1024], str[1024];
    int id = 0;
    MYSQL *mysql;
    int mysql_timeout = 10;

    mysql = mysql_init( NULL ); mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout ); sleep( 2 );

    if ( !mysql_real_connect( mysql, "localhost", "root", "wangwei","wang", 0, NULL, 0 ) )
    {
    printf("error");
    exit(1);
    }

    while ( 1 )
    {
    snprintf( str, 1024, "wangwei %d", id );

    snprintf( sql, 1024, "insert into test values (%ld,'%s')", id ++, str ); if ( mysql_query( mysql, sql ) != 0 )
    {
    printf( "1 mysql:%s\n", mysql_error( mysql ) );
    }

    printf("+");


    return arg;
    }
    main()
    {
    int status;
    pthread_t id1, id2; printf("mysql_thread_safe = %d\n",mysql_thread_safe());
    status = pthread_create( &id1, NULL, test1, NULL );

    if (status != 0)
    {
    perror("thread1");
    exit(0);
    } status = pthread_create( &id2, NULL, test2, NULL ); if (status != 0)
    {
    perror("thread2");
    exit(0);
    } while(1);
    }
    家里电脑180M   公司电脑小一些  很奇怪  凡是连mysql的程序  个个都100多m 诶