OSGeo / PROJ

PROJ - Cartographic Projections and Coordinate Transformations Library
https://proj.org
Other
1.73k stars 783 forks source link

Caching PJ objects consumes a lot of memory, proj_create_from_wkt Is there a memory leak #2941

Closed cejutue closed 2 years ago

cejutue commented 2 years ago

Problem description

I have an epsg.txt file, which stores 5000 + wkt. My system wants to create PJ objects and cache them, but they actually consume 600m memory and take 10s, which greatly affects the system efficiency. I don't know if I used it wrong

#include <fstream>
#include <map>
#include "proj.h"
#pragma comment(lib,"proj_d.lib")
#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"sqlite3.lib")
int  testCreateProj()
{
    const char* const options[] = { "STRICT=NO", nullptr };

    //open epsg.txt
    std::ifstream f("D:\\source\\kernel\\data\\coordinatesystem\\EPSG.txt");
    std::string strLine;
    std::getline(f, strLine);

    //read epsg
    int nCount = _atoi64(strLine.c_str());

    std::map<int, PJ*> m_PJ_map;
    auto ctx = proj_context_create();
    while (nCount > 0)
    {
        PROJ_STRING_LIST warnings = nullptr;
        PROJ_STRING_LIST errors = nullptr;

        strLine.clear();
        std::getline(f, strLine);
        const char* strEPSG = strLine.data();
        strEPSG = strLine.data() + 5;

        int nEPSG = ::_atoi64(strEPSG);
        if (nEPSG <= 0)
            continue;

        std::string wkt;
        std::getline(f, wkt);

        PJ* a = proj_create_from_wkt(ctx, wkt.c_str(), options, &warnings, &errors);
        nCount--;

        m_PJ_map[nEPSG] = a;    
        proj_string_list_destroy(warnings);
        proj_string_list_destroy(errors);
    }   
    proj_context_destroy(ctx);
    f.close();
    return true;
}

Expected Output

none

Environment Information

Installation method

rouault commented 2 years ago

Can you provide a link to EPSG.txt (or attach it here) ?

cejutue commented 2 years ago

您能否提供指向 EPSG.txt 的链接(或在此处附上)?

https://github.com/GeoStar2018/gskernel/blob/master/data/coordinatesystem/EPSG.txt

cejutue commented 2 years ago

do not create PJ object , only use wkt string show in UI