;+ ; ; Laboratory for Atmospheric and Space Physics ; University of Colorado, Boulder, Colorado, USA ; ; AUTHOR: ; Lon Riesberg ; ; DATE: March 26. 2007 ; ; PURPOSE: ; Keeps track of memory usage and garbage collects if ; memory waste is 50 MB+. Expects that calling code ; is actively managing its resources and memory is ; checked after releasing unused resources. This ; uses common blocks so it can be quickly added ; anywhere but the usefulness of check_memory will ; quickly deteriorate if multiple procedures are ; using it simulataneously. Use the reset keyword ; to allow a new procedure to use check_memory. ; ; NOTESa; ; ; USAGE EXAMPLE: ; check_memory ;- pro check_memory, reset=reset COMMON MEMORY_MONITOR, mem_history if keyword_set(reset) and (n_elements(mem_history) gt 0) then begin mem_history = temporary(mem_history) return endif if n_elements(mem_history) eq 0 then begin mem_history = ptr_new() if get_debug_level() gt 9 then device, decomposed=0 endif curr_mem = memory(/current) / 1E6 print, 'memory currently in use: ', curr_mem, ' MB' if ~ptr_valid(mem_history) then begin mem_history = ptr_new(curr_mem) endif else begin *mem_history = [*mem_history, curr_mem] if get_debug_level() gt 9 then begin plot, *mem_history, title='CIPS Processing Memory Use', xtitle='iteration', ytitle='MB', $ xticks=1, min_value=0 endif n_history = n_elements(*mem_history) mem_leak = (*mem_history)[n_history-1] - (*mem_history)[0] if mem_leak gt 50 then begin heap_gc print, 'memory released: ', curr_mem-(memory(/current)/1E6), ' MB' endif endelse return end