takahiro_kamiyaesrij-esridist

Memory is not freed

Discussion created by takahiro_kamiyaesrij-esridist Employee on Aug 4, 2017
Latest reply on Aug 18, 2017 by takahiro_kamiyaesrij-esridist

Hello,

 

Drop it on the map with GraphicsOverlay,
If you delete it with Clear,
I am in trouble that the memory will not be released.

 

Do you have any problems?

 

// Copyright 2016 ESRI                                                                                                                                                                                    
//                                                                                                                                                                                   
// All rights reserved under the copyright laws of the United States                                                                                                                                                                                   
// and applicable international laws, treaties, and conventions.                                                                                                                                                                                   
//                                                                                                                                                                                   
// You may freely redistribute and use this sample code, with or                                                                                                                                                                                   
// without modification, provided you include the original copyright                                                                                                                                                                                   
// notice and use restrictions.                                                                                                                                                                                   
//                                                                                                                                                                                   
// See the Sample code usage restrictions document for further information.                                                                                                                                                                                   
//                                                                                                                                                                                   
#include <sys/resource.h>                                                                                                                                                                                   
                                                                                                                                                                                   
#include "ArcGISTiledLayer.h"                                                                                                                                                                                   
#include "Basemap.h"                                                                                                                                                                                   
#include "Map.h"                                                                                                                                                                                   
                                                                                                                                                                                   
#include "PolygonBuilder.h"                                                                                                                                                                                   
#include "PolylineBuilder.h"                                                                                                                                                                                   
#include "SimpleFillSymbol.h"                                                                                                                                                                                   
#include "SimpleLineSymbol.h"                                                                                                                                                                                   
                                                                                                                                                                                   
#include "ArcGIS.h"                                                                                                                                                                                   
                                                                                                                                                                                   
ArcGIS::ArcGIS(QWidget *parent /*=nullptr*/):                                                                                                                                                                                   
    QMainWindow(parent)                                                                                                                                                                                   
{                                                                                                                                                                                   
    SpatialReference sr(30165);                                                                                                                                                                                   
    MapGraphicsView *m_mapView = new MapGraphicsView(this);                                                                                                                                                                                   
                                                                                                                                                                                   
    ArcGISTiledLayer *tiledLayer = new ArcGISTiledLayer(new TileCache("/users/test/gis001/tpk/16000-4000.tpk", this), this);                                                                                                                                                                                   
    tiledLayer->setVisible(true);                                                                                                                                                                                   
    tiledLayer->setOpacity(0.8);                                                                                                                                                                                   
                                                                                                                                                                                   
    Map *m_map = new Map(new Basemap(tiledLayer, this), this);                                                                                                                                                                                   
    m_map->setInitialViewpoint(Viewpoint(Point(110867.18539903179,-106473.50196292577, sr), 10000.0));                                                                                                                                                                                   
    m_mapView->setMap(m_map);                                                                                                                                                                                   
                                                                                                                                                                                   
    GraphicsOverlay *graphicsOverlay = new GraphicsOverlay(this);                                                                                                                                                                                   
    graphicsOverlay->setRenderingMode(GraphicsRenderingMode::Static);                                                                                                                                                                                   
    m_mapView->graphicsOverlays()->append(graphicsOverlay);                                                                                                                                                                                   
                                                                                                                                                                                   
    setCentralWidget(m_mapView);                                                                                                                                                                                   
                                                                                                                                                                                   
    // グラフィック生成前メモリ使用量を表示
    // Display memory usage before graphic generation                                                                                                                                                                                   
    print();                                                                                                                                                                                   
                                                                                                                                                                                   
                                                                                                                                                                                   
    // 10000回グラフィックを生成(三角形+塗り潰し)                                                                                                                                                                                   
    for (int i = 0; i < 10000; ++i) {                                                                                                                                                                                   
        PolygonBuilder polyBUilder(sr);                                                                                                                                                                                   
        polyBUilder.addPoint(Point(110867.18539903179 + i * 10, -106473.50196292577 + i * 10, sr));                                                                                                                                                                                   
        polyBUilder.addPoint(Point(110865.2853990318  + i * 10, -106474.8019629257  + i * 10, sr));                                                                                                                                                                                   
        polyBUilder.addPoint(Point(110865.48539903181 + i * 10, -106475.6019629257  + i * 10, sr));                                                                                                                                                                                   
        polyBUilder.addPoint(Point(110868.8853990318  + i * 10, -106488.10196292578 + i * 10, sr));                                                                                                                                                                                   
                                                                                                                                                                                   
        SimpleFillSymbol *nestingSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor("#FF008000"), this);                                                                                                                                                                                   
        Graphic *g = new Graphic(polyBUilder.toGeometry(), nestingSymbol, this);                                                                                                                                                                                   
        graphicsOverlay->graphics()->append(g);                                                                                                                                                                                   
    }                                                                                                                                                                                   
                                                                                                                                                                                   
    // グラフィック生成後メモリ使用量を表示
    // Display memory usage before graphic generation                                                                                                                                                                                   
    print();                                                                                                                                                                                   
                                                                                                                                                                                   
                                                                                                                                                                                   
#if 0                                                                                                                                                                                   
                                                                                                                                                                                   
    // 削除パターン1  GraphicListModelをclearする                                                                                                                                                                                   
    graphicsOverlay->graphics()->clear();                                                                                                                                                                                   
                                                                                                                                                                                   
    // グラフィック削除後メモリ使用量を表示                                                                                                                                                                                   
    // Display memory usage after graphic deletion
    print();                                                                                                                                                                                   
                                                                                                                                                                                   
    // 結果 -> メモリ使用量変わらず                                                                                                                                                                                   
    // result -> memory usage remains unchanged                                                                                                                                                                              
#elif 0                                                                                                                                                                                   
                                                                                                                                                                                   
    // 削除パターン2  GraphicListModelから1個ずつremoveする                                                                                                                                                                                   
    GraphicListModel *model = graphicsOverlay->graphics();                                                                                                                                                                                   
                                                                                                                                                                                   
    while (model->size() > 0) {                                                                                                                                                                                   
        Graphic *g = model->first();                                                                                                                                                                                   
        model->removeOne(g);                                                                                                                                                                                   
    }                                                                                                                                                                                   
                                                                                                                                                                                   
    // グラフィック削除後メモリ使用量を表示                                                                                                                                                                                   
    // Display memory usage after graphic deletion
    print();                                                                                                                                                                                   
                                                                                                                                                                                   
    // 結果 -> メモリ使用量変わらず                                                                                                                                                                                   
    // result -> memory usage remains unchanged                                                                                                                                                                                   
#elif 0                                                                                                                                                                                   
                                                                                                                                                                                   
    // 削除パターン3  GraphicListModelから1個ずつremoveし、さらにGraphicを自前でdeleteする                                                                                                                                                                                   
    GraphicListModel *model = graphicsOverlay->graphics();                                                                                                                                                                                   
                                                                                                                                                                                   
    while (model->size() > 0) {                                                                                                                                                                                   
        Graphic *g = model->first();                                                                                                                                                                                   
        model->removeOne(g);                                                                                                                                                                                   
        delete g;                                                                                                                                                                                   
    }                                                                                                                                                                                   
                                                                                                                                                                                   
    // グラフィック削除後メモリ使用量を表示                                                                                                                                                                                   
    // Display memory usage after graphic deletion
    print();                                                                                                                                                                                   
                                                                                                                                                                                   
    // 結果 -> メモリ使用量変わらず                                                                                                                                                                                   
    // result -> memory usage remains unchanged                                                                                                                                                                                        
#elif 0                                                                                                                                                                                   
                                                                                                                                                                                   
    // 削除パターン4  GraphicListModelから1個ずつremoveし、さらにGraphicとその中のSymbolを自前でdeleteする                                                                                                                                                                                   
    GraphicListModel *model = graphicsOverlay->graphics();                                                                                                                                                                                   
                                                                                                                                                                                   
    while (model->size() > 0) {                                                                                                                                                                                   
        Graphic *g = model->first();                                                                                                                                                                                   
        Symbol *sls = g->symbol();                                                                                                                                                                                   
        model->removeOne(g);                                                                                                                                                                                   
                                                                                                                                                                                   
        g->setSymbol(NULL);                                                                                                                                                                                   
        delete sls;                                                                                                                                                                                   
                                                                                                                                                                                   
        delete g;                                                                                                                                                                                   
    }                                                                                                                                                                                   
                                                                                                                                                                                   
    // グラフィック削除後メモリ使用量を表示
    // Display memory usage after graphic deletion                                                                                                                                                                                  
    print();                                                                                                                                                                                   
                                                                                                                                                                                   
    // 結果 -> メモリ使用量変わらず                                                                                                                                                                                   
    // result -> memory usage remains unchanged                                                                                                                                                                                   
#elif 1                                                                                                                                                                                   
                                                                                                                                                                                   
    // 削除パターン5  GraphicListModelから1個ずつremoveし、さらにGraphicとその中のSymbolを自前でdeleteし、                                                                                                                                                                                   
    //              加えてGraphicsOverlayまでも削除する                                                                                                                                                                                   
    GraphicListModel *model = graphicsOverlay->graphics();                                                                                                                                                                                   
                                                                                                                                                                                   
    while (model->size() > 0) {                                                                                                                                                                                   
        Graphic *g = model->first();                                                                                                                                                                                   
        Symbol *sls = g->symbol();                                                                                                                                                                                   
        model->removeOne(g);                                                                                                                                                                                   
                                                                                                                                                                                   
        g->setSymbol(NULL);                                                                                                                                                                                   
        delete sls;                                                                                                                                                                                   
                                                                                                                                                                                   
        delete g;                                                                                                                                                                                   
    }                                                                                                                                                                                   
                                                                                                                                                                                   
    m_mapView->graphicsOverlays()->removeOne(graphicsOverlay);                                                                                                                                                                                   
    delete graphicsOverlay;                                                                                                                                                                                   
                                                                                                                                                                                   
    // グラフィック削除後メモリ使用量を表示
    // Display memory usage after graphic deletion                                                                                                                                                                                   
    print();                                                                                                                                                                                   
                                                                                                                                                                                   
    // 結果 -> メモリ使用量変わらず                                                                                                                                                                                   
    // result -> memory usage remains unchanged                                                                                                                                                                                        
#endif                                                                                                                                                                                   
}                                                                                                                                                                                   
                                                                                                                                                                                   
void ArcGIS::print()                                                                                                                                                                                   
{                                                                                                                                                                                   
    struct rusage psinfo;                                                                                                                                                                                   
                                                                                                                                                                                   
    if (getrusage(RUSAGE_SELF, &psinfo) == 0) {                                                                                                                                                                                   
        fprintf(stderr, "memory usage : %ld kb\n", psinfo.ru_maxrss);                                                                                                                                                                                   
    }                                                                                                                                                                                   
}                                                                                                                                                                                   

 

// Copyright 2016 ESRI                                                                                                         
//                                                                                                        
// All rights reserved under the copyright laws of the United States                                                                                                        
// and applicable international laws, treaties, and conventions.                                                                                                        
//                                                                                                        
// You may freely redistribute and use this sample code, with or                                                                                                        
// without modification, provided you include the original copyright                                                                                                        
// notice and use restrictions.                                                                                                        
//                                                                                                        
// See the Sample code usage restrictions document for further information.                                                                                                        
//                                                                                                        
                                                                                                        
#ifndef ARCGIS_H                                                                                                        
#define ARCGIS_H                                                                                                        
                                                                                                        
#include <QMainWindow>                                                                                                        
#include "MapGraphicsView.h"                                                                                                        
                                                                                                        
using namespace Esri::ArcGISRuntime;                                                                                                        
                                                                                                        
class ArcGIS : public QMainWindow                                                                                                        
{                                                                                                        
    Q_OBJECT                                                                                                        
public:                                                                                                        
    ArcGIS(QWidget *parent = nullptr);                                                                                                        
                                                                                                        
private:                                                                                                        
    void print();                                                                                                        
};                                                                                                        
                                                                                                        
#endif // ARCGIS_H                                                                                                        

Outcomes