Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Memory leak cplex c++

    Posted 05/26/15 01:04 PM

    Originally posted by: optimiz


    Hi,
     
    I have a Cplex C++ code and my model looks like:

    IloEnv env;
    try{
    IloNumVarArray x(env, m, 0, 0);
    IloRange c1(env);
    IloRangeArray c2(env, m, 0, 0);
    ...
    for(){
    model.add(c2[i]);
    model.add(x[i]);
    }
    model.add(c1);
    ...
    model.add(obj);
    IloCplex cplex(model);
    cplex.setOut(env.getNullStream());
    cplex.solve();
    c2.end();
    c1.end();
    x.end();
    model.end();
    cplex.end();
    env.end();
    
    if (cplex.getStatus() == IloAlgorithm::Infeasible)
         env.out() << "No Solution" << endl;
    
    }
    catch (IloException& ex) {
         cerr << "Error: " << ex << endl;
    }
    catch (...) {
        cerr << "Error" << endl;
    }
    

     

     
    I still have memory leak and my model is terminated. How do I eliminate the leak even when I solve the model only once?
     
    Thanks

    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Memory leak cplex c++

    Posted 05/27/15 02:00 AM

    Where is your leak and how do you assess that you have a leak?

    There are several problems with your code anyway:

    1. Calling env.end() should be enough to release all memory. There is no need to explicitly end() the other objects (unless you want get them destroyed earlier).
    2. Calling cplex.getStatus() after calling cplex.end() or env.end() gives undefined behavior: the IloCplex instance has already been deleted.
    3. You are not calling env.end() in case an exception is thrown.

    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Memory leak cplex c++

    Posted 05/27/15 02:14 AM

    Originally posted by: optimiz


    When I keep track of the memory usage, it increases up to a point and then stays the same. However, sometimes it is automatically killed. I used valgrind to see if there is a leak and there is "still reachable" type of leak, the others are all 0.

     

    Also, now I have:

    catch (IloException& ex) {
    env.end();    
     cerr << "Error: " << ex << endl;
    }

    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Memory leak cplex c++

    Posted 06/02/15 07:48 AM

    'still reachable' usually does not point at a real leak. What are the things that are still reachable?

    How do you "keep track of the memory usage"?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Memory leak cplex c++

    Posted 09/03/19 08:58 AM

    Originally posted by: bermeom


    Hello, I am working with ILOG CPLEX Studio 128 in C ++ and I have problems when the API tries to deallocate memory since it never releases and increases. My code consists of a Controller class that it has a description of my MIP using the CPLEX API, and another called ControlSystem class that It has an attribute of type Controller, therefore, ControlSystem calls the Controller's solver method to solve the problem with specific parameters. After a while, it is necessary to update the object controller, then I have explored two possibilities:

    • Update only some problem parameters of the same MIP description, that is, in my particular case, I need to update an IloNumExprArray, which is used in the objective function. But with the following code the memoery of the old expression not are released
    class Controller{
            IloEnv env;
            IloNumExprArray c;
            setup(){
                c = IloExprArray(this->env);
                ...
            }
            update(..){
                c.end(); // memory is not released 
                c = IloExprArray(this->env);// and increases when a new one is created
                 ..
                   
            }
    }
    
    • Therefore, my second solution is using two pointer of Controller class when it is necessary to update, the second pointer  will create a new controller and then be exchanged. Thus, the old controller object is deleted. This solution works sometimes and sometimes not, so after a while, the program consumes all the memory of my computer. This is an example of my code:
    class ControllerSystem{
            Controller controller1*;
            Controller controller2*;
            ControllerSystem(..){
                    this->controller1 = new Controller(..);
                    this->controller2 = NULL;
                    ..
            }
            
            updateController(..){
                this->controller2 = new Controller(..);    
                Controller aux = this->controller1;
                this->controller1 =  controller2;
                this->controller2 =  aux;
                delete this->controller2;
                this->controller2 = NULL;
            }
            ...
        
    }
    
    class Controller {
        IloEnv env;
        IloModel model;
        IloCplex cplex;
        Controller(..){
            ..
            setup();
        }
    
        ~Controller(..){
            this->cplex.end();
            this->model.end();
            this->env.end();
        }
    }
    

    Some forums recommend using Valgrind or any tool to detect these types of memory problems. Therefore, I used Valgrind and got these results:

    class Controller {
     
     IloExtractableArray refConstraints;
        ..
        
    void setup(..){
        ..
        this->refConstraints.add(this->model.add(IloMinimize(this->env, IloScalProd(this->c,this->vars) )));
        // This line produce the following Valgrind output:
        ==25031== Warning: set address range perms: large range [0xdc2ffe70, 0xeef3dca0) (undefined)
        ==25031== Warning: set address range perms: large range [0x93644028, 0xa6281e88) (noaccess)
        ==25031== Warning: set address range perms: large range [0x101b7be70, 0x1147b9ca0) (undefined)
        ==25031== Warning: set address range perms: large range [0xa6282028, 0xb8ebfe88) (noaccess)
        ..
        this->cplex = IloCplex(this->model);// This line produce the following Valgrind output:
        ==25031== Warning: set address range perms: large range [0x9364a040, 0xa6281e70) (undefined)
        ==25031== Warning: set address range perms: large range [0xa6288040, 0xb8ebfe70) (undefined)
    
    }
        ..
    }
    

    So, I would like to know what is the correct way to eliminate a memory problem. I know that memory is not deleted immediately after using env.end (); but I give the program time to free the memory and that never happens, it just increases.

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Memory leak cplex c++

    Posted 09/03/19 10:11 AM

    Have you tried calling c.endElements() in addition to just c.end()? Are you sure that the leak comes from c and only from c?


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Memory leak cplex c++

    Posted 09/03/19 01:33 PM

    Originally posted by: bermeom


    Yes and that does not work. But I have a doubt, how to reset an IloNumExpr object?, i,e, set the value 0  since this object does not have the clear() method. Although I can cast that variable to IloExpr and use the clear method, the memory does not released and increased


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Memory leak cplex c++

    Posted 09/06/19 02:29 AM

    The method to end something is called end(), not clear().


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Memory leak cplex c++

    Posted 09/06/19 08:10 AM

    Originally posted by: bermeom


    Yes, I know. As you can see in the code above, I have already used it, but it isn't working. So, how I can to release the memory used by an IloNumExpr object ?.

    Could you check this problem creating a huge expression in an IloNumExpr object and use .end(). 


    #CPLEXOptimizers
    #DecisionOptimization