Memory issues when exporting large multiport simulations

As the title states I currently have issues with exporting large multiport simulations. After about 9 out of 16 ports my memory is full and the python script crashes. I tried calling the garbage collection manually, but this does not free any memory. Does someone know a solution to free allocated memory in sim4life (version 3.4)?

Cheers!

Hi Peter, the short answer is that you can't free this allocated memory. This is a known issue with Sim4Life's Python API. What happens is that the postprocessing algorithms instantiated by the Python functions create some C++ data objects to hold the actual bulk of the data: the same memory blocks can be accessed by Sim4Life's C++ code, or by the user's Python script. Unfortunately, deleting the Python objects does not always deallocate the C++ data objects. This can result in memory leaks, similar to what you described.

The workaround is to not allocate too much memory in the first place, by reusing existing algorithms instead of creating new ones. It is possible to only change the inputs of an algorithm and update it. Here is an example that exports the E fields of each port of a multiport simulation as Matlab files, but only allocate memory for one port:

	# Creating the analysis pipeline
	# Adding a new EmMultiPortSimulationExtractor
	simulation = document.AllSimulations["Patch Antenna - Multiport"]
	em_multi_port_simulation_extractor = simulation.Results()

	#Create the postprocessing pipeline once
	output_port = em_multi_port_simulation_extractor.Outputs[0]
	# Adding a new EmPortSimulationExtractor
	em_port_simulation_extractor = analysis.extractors.EmPortSimulationExtractor(inputs=[output_port])
	em_port_simulation_extractor.UpdateAttributes()
	document.AllAlgorithms.Add(em_port_simulation_extractor)

	# Adding a new EmSensorExtractor
	em_sensor_extractor = em_port_simulation_extractor["Overall Field"]

	document.AllAlgorithms.Add(em_sensor_extractor)

	# Adding a new MatlabExporter
	inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
	matlab_exporter = analysis.exporters.MatlabExporter(inputs=inputs)
	matlab_exporter.UpdateAttributes()
	document.AllAlgorithms.Add(matlab_exporter)
		
		
	# Update the postprocessing pipeline for each port
	for i, output_port in enumerate(em_multi_port_simulation_extractor.Outputs):
		em_port_simulation_extractor.raw.SetInputConnection(0, output_port.raw) # this is the main "trick"
		em_port_simulation_extractor.UpdateAttributes()

		inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
		matlab_exporter.FileName = u"D:\\temp\\ExportedData_{}.mat".format(i)
		print matlab_exporter.FileName
		matlab_exporter.UpdateAttributes()
		matlab_exporter.Update()

Okay awesome, I think this will do the trick :)

Log in to reply