Brethlosze
Brethlosze

Reputation: 1622

Matlab - How to Limit Access to Resources

Lets say i have to run my func.m for several hours, letting the Matlab running on a server (Windows). I wish the function not to use the 99% of the available CPU, Disk, and/or Memory as indicated by the Task Manager, lets say, limit to 75% each.

How should i limit them, for reserving another instance of Matlab or other processes without any issue?

Upvotes: 2

Views: 60

Answers (1)

kedarps
kedarps

Reputation: 861

You can use the memory function to do something similar to this,

% set threshold here
mem_threshold = 75;

while 1
     % do stuff

     %%
     % check how much memory is being used
     [~ sys] = memory; 
     mem_avail = sys.PhysicalMemory.Available;
     mem_total = sys.PhysicalMemory.Total;
     mem_used = 100*(mem_total-mem_avail)/mem_total;

     % break loop, if memory used exceeds threshold         
     if mem_used > mem_threshold
        break;
     end
end

Upvotes: 1

Related Questions