Skip to main content

Logging Memory Consumption

Answered

Comments

5 comments

  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    You can measure memory usage using some other tool: e.g. https://github.com/astrofrog/psrecord.

    With this you can even get a nice plot with memory consumption as your python script runs:

    psrecord "gurobi_cl model.mps" --include-child --interval 0.1 --log activity.txt --plot plot.png

    Cheers, 
    David

    0
  • Mahnoor Naseer
    First Comment
    First Question

    Hey David, thank you for your response, this is really helpful.

    0
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    HI Mahnoor,

    Here's another way using callbacks (see also Callback Codes)

    import sys
    from functools import partial

    import gurobipy as gp
    from gurobipy import GRB


    def mycallback(model, where, *, cbdata):
        if where != GRB.Callback.POLLING:
            runtime = model.cbGet(GRB.Callback.RUNTIME)
            memused = model.cbGet(GRB.Callback.MEMUSED)
            cbdata[runtime] = memused


    memusage = {}
    with gp.read(sys.argv[1]) as m:
        callback_func = partial(mycallback, cbdata=memusage)
        m.optimize(callback_func)

    print(memusage)

    Cheers, 
    David

    1
  • Mahnoor Naseer
    First Comment
    First Question

    Thank you so much for the updated response David. Both the methods are very useful for my current application. Cheers!

     

     

    0
  • Mahnoor Naseer
    First Comment
    First Question

    Hello,

    Just an addition to this thread which I think others might find helpful too.

    So based on the two methods suggested by David above, I ran my simulations. Both of them do a great job, however, for someone who is looking to compare the performance of different MILP models based on the internal memory usage during a MILP optimization (MIP iterations), the callback method is more efficient.

    psrecord does the job too, but it externally records the memory usage of a running Gurobi optimization process, so the events are system-defined rather than solver-defined. This means that callback logging aligns with solver events (e.g., MIP iterations, node exploration, simplex steps), while psrecord logs at fixed time intervals regardless of what the solver is doing.

    I hope my conclusion is correct, will appreciate any additions/ corrections.

     

    Thank you. 


    1

Please sign in to leave a comment.