Hi I am getting massive memory usage from my python program.
I have simplified my code, here is the main function
points = []
read_pcd(TEST1_PCD, points)
del points[:] # i also tried del points
# program exits here
the problem is my point-cloud data set is large, ~ 1 million points. I process it it and covert it into an elevation map so I no longer need the points... however the memory allocated to the points remain. I have tried del points as well. As you can see in the memory profiler that del the points only frees 7 Mb.... Does python not bother to free the memory that the list elements occupied? Because I am worried about running out of memory later in my project.
This is the memory profiler I used https://pypi.python.org/pypi/memory_profiler
here is the read_pcd function for refference
def read_pcd(fname, points):
data_start = False
with open(fname, 'r') as f:
for i, line in enumerate(f):
words = line.split()
if words[0] == "DATA":
data_start = True
elif data_start == True:
point = Point(float(words[0]), float(words[1]), float(words[2]))
points.append(point)
Line # Mem usage Increment Line Contents
================================================
17 23.559 MiB 0.000 MiB @profile
18 def main():
19 24.121 MiB 0.562 MiB rospy.init_node('traversability_analysis_node')
20 24.129 MiB 0.008 MiB points = []
21 1322.910 MiB 1298.781 MiB read_pcd(TEST1_PCD, points)
22 1315.004 MiB -7.906 MiB del points[:]
class Point(object):
def __init__(self, x=0.0, y=0.0, z=0.0, intensity=255, cart=True, range_m=0, az=0, elv=0):
# http://www.mathworks.com.au/help/matlab/ref/cart2sph.html
DEG2RAD = m.pi/180.0
if cart == True:
self.x = x
self.y = y
self.z = z
self.intensity = intensity
self.range = np.sqrt(x**2 + y**2 + z**2)
self.azimuth = np.arctan2(y, x)
r_xy = np.sqrt(x**2 + y**2)
self.elvation = np.arctan2(z, r_xy )
else:
elv = elv*DEG2RAD
az = az*DEG2RAD
self.x = range_m*np.cos(elv)*np.cos(az)
self.y = range_m*np.cos(elv)*np.sin(az)
self.z = range_m*np.sin(elv)
self.range = range_m
self.azimuth = az
self.elvation = elv
self.intensity = intensity
profiler output when calling gc.collect
Line # Mem usage Increment Line Contents
================================================
18 23.555 MiB 0.000 MiB @profile
19 def main():
20 24.117 MiB 0.562 MiB rospy.init_node('traversability_analysis_node')
21 24.125 MiB 0.008 MiB points = []
22 1322.914 MiB 1298.789 MiB read_pcd(TEST1_PCD, points)
23 1322.914 MiB 0.000 MiB gc.collect()
24 1315.008 MiB -7.906 MiB del points
25 1315.008 MiB 0.000 MiB time.sleep(5)