un44444444
unread,Sep 27, 2010, 9:46:43 AM9/27/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongodb-dev
I got a memory leak problem in mongodb, when query data using php with
cursor.
but it goes OK with my c++ client.
Then i use tcpdump to capture the packages, and find that c++ client
send 11 package while php send 10.
the one missing is OP_KILL_CURSORS
So i add a killCursor() method in MongoCursor.
It seems memory leak problem fix with below code:
<code>
$db = $connection->selectDB($database_names);
$collection = $db->selectCollection($table_name);
$cursor = $collection->find($query,array("UID"))->limit($limit_size);
while($cursor->hasNext())
{
$user = $cursor->getNext();
var_dump($user);
}
$cursor->killCursor();
unset($cursor);
</code>
cursor.patch:
<pre>
diff --git a/cursor.c b/cursor.c
index 7efc5fa..26cacfd 100644
--- a/cursor.c
+++ b/cursor.c
@@ -810,6 +810,31 @@ PHP_METHOD(MongoCursor, count) {
zval_ptr_dtor(&db_z);
}
+/* {{{ MongoCursor::killCursor
+ */
+PHP_METHOD(MongoCursor, killCursor) {
+ mongo_cursor *cursor =
(mongo_cursor*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ char quickbuf[128];
+ buffer buf;
+ zval temp;
+
+ //If the cursor_id is 0, the db is out of results anyway.
+ if (cursor->cursor_id == 0) {
+ return;
+ }
+
+ //
+ buf.pos = quickbuf;
+ buf.start = buf.pos;
+ buf.end = buf.start + 128;
+
+ php_mongo_write_kill_cursors(&buf, cursor TSRMLS_CC);
+ mongo_say(cursor->link, &buf, &temp TSRMLS_CC);
+
+ cursor->cursor_id = 0;
+}
+/* }}} */
+
static function_entry MongoCursor_methods[] = {
PHP_ME(MongoCursor, __construct, NULL, ZEND_ACC_CTOR|
ZEND_ACC_PUBLIC)
PHP_ME(MongoCursor, hasNext, NULL, ZEND_ACC_PUBLIC)
@@ -828,6 +853,7 @@ static function_entry MongoCursor_methods[] = {
PHP_ME(MongoCursor, sort, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoCursor, hint, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoCursor, addOption, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(MongoCursor, killCursor, NULL, ZEND_ACC_PUBLIC)
/* query */
PHP_ME(MongoCursor, doQuery, NULL, ZEND_ACC_PROTECTED)
diff --git a/cursor.h b/cursor.h
index 1be561e..507ac44 100644
--- a/cursor.h
+++ b/cursor.h
@@ -45,6 +45,7 @@ PHP_METHOD(MongoCursor, valid);
PHP_METHOD(MongoCursor, reset);
PHP_METHOD(MongoCursor, count);
PHP_METHOD(MongoCursor, info);
+PHP_METHOD(MongoCursor, killCursor);
#define preiteration_setup mongo_cursor
*cursor; \
PHP_MONGO_GET_CURSOR(getThis());
\
</pre>