有没有办法在cython程序中找到内存泄漏?我有一个如下的cython代码...如何检测究竟是什么导致内存泄漏? 以下代码会导致内存泄漏吗? 有没有办法确保没有内存泄漏?
cdef extern from "stdlib.h":
double drand48()
import cython
import numpy as np
cimport numpy as np
def draw_topic(double[:] topic_distribution):
cdef double td_sum = 0
cdef double rand_01 = drand48()
cdef double accumulated_weight = 0
cdef long drawn_topic = len(topic_distribution) - 1
for i in xrange(len(topic_distribution)):
td_sum += topic_distribution[i]
for i in xrange(len(topic_distribution)):
accumulated_weight += topic_distribution[i] / td_sum
if rand_01 <= accumulated_weight:
drawn_topic = i
break
return drawn_topic
@cython.boundscheck(False)
def gs_iterate_once(double[:,:] doc_topic,
double[:,:] topic_word,
double[:] topic_distribution,
double[:] topic_probabilities,
unsigned int[:,:] doc_word_topic,
int num_topics):
cdef unsigned int doc_id
cdef unsigned int word_id
cdef unsigned int topic_id
cdef unsigned int new_topic
for i in xrange(doc_word_topic.shape[0]):
doc_id = doc_word_topic[i, 0]
word_id = doc_word_topic[i, 1]
topic_id = doc_word_topic[i, 2]
doc_topic[doc_id, topic_id] -= 1
topic_word[topic_id, word_id] -= 1
topic_distribution[topic_id] -= 1
for j in xrange(num_topics):
# 1. For every topic get current topic distribution for the document form
# doc_topic.
# 2. From topic_word get current distribution of that word across
# topics.
# Get the un-normalized probability of a word being assigned new topic
# ==> element-wise (1 * 2 / topic_distribution)
topic_probabilities[j] = (doc_topic[doc_id, j] * topic_word[j, word_id]) / topic_distribution[j]
new_topic = draw_topic(topic_probabilities)
doc_topic[doc_id, new_topic] += 1
topic_word[new_topic, word_id] += 1
topic_distribution[new_topic] += 1
# Set the new topic
doc_word_topic[i, 2] = new_topic