##Copyright 2010 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##GNU General Public License for more details.
##
##You should have received a copy of the GNU General Public License
##along with pythonOCC.  If not, see <http://www.gnu.org/licenses/>.

''' Test impact of MMGT_OPT flag'''

import os
from OCC.BRepPrimAPI import *
import time

def mem(size="rss"):
    """Generalization; memory sizes: rss, rsz, vsz."""
    return int(os.popen('ps -p %d -o %s | tail -1' %
                        (os.getpid(), size)).read())
 
def rss():
    """Return ps -o rss (resident) memory in kB."""
    return float(mem("rss"))/1024

def make_box():
	return BRepPrimAPI_MakeBox(5,5,5).Shape()
	
def make_n_boxes(n):
    print 'Creating %i boxes...'%n,
    for i in range(n):
        box_shape = BRepPrimAPI_MakeBox(5,5,5).Shape()
    print 'done.'
    
def test():
	''' Test MMGT_OPT impact. When MMGT_OPT is set to 1, OCC allocates blocks. When set to 0 ,memory is freed when objects are deleted.
	'''
	initial_memory = rss()
	# make 10000 boxes
	make_n_boxes(10000)
	print 'Consumed memory: %f Mb before deleting objects'%(rss()-initial_memory)
	print 'deleting objects...',
	init_time = time.time()
	GarbageCollector.garbage.smart_purge()
	print 'done in %fs.'%(time.time()-init_time)
	print 'Consumed memory: %f Mb after deleting objects'%(rss()-initial_memory)

test()