8000 hScan() memoty leak · Issue #501 · phpredis/phpredis · GitHub
[go: up one dir, main page]

Skip to content

hScan() memoty leak #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aartua opened this issue Sep 1, 2014 · 4 comments
Closed

hScan() memoty leak #501

aartua opened this issue Sep 1, 2014 · 4 comments

Comments

@aartua
Copy link
aartua commented Sep 1, 2014
pconnect('127.0.0.1', 6379); $redis->select(2); $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); ``` for ($i = 0; $i < 3000000; $i++) { $redis->hIncrBy('tableName', md5(microtime().rand(1000, 9999999)), time()); } $start_memory = memory_get_usage(); echo 'Memory ('.$start_memory.')'.PHP_EOL; $it = null; while($data = $redis->hScan('tableName', $it, '1*', 100)) { unset($data); } echo 'Memory ('.(memory_get_usage() - $start_memory).')'.PHP_EOL; unset($redis); echo 'Memory ('.(memory_get_usage() - $start_memory).')'.PHP_EOL; ``` ?>

RESULT:
Memory (867992)
Memory (3707416)
Memory (3706576)

@michael-grunder
Copy link
Member

Hey there,

This is a great find! I've confirmed that this is in fact the case. The actual leak is here:

[Tue Sep 2 11:53:05 2014] Script: '/Users/mgrunder/dev/phpredis/hscan.php' /Users/mgrunder/dev/php-5.5.13/ext/redis/library.c(994) : Freeing 0x109DC70D8 (72 bytes), script=/Users/mgrunder/dev/phpredis/hscan.php /Users/mgrunder/dev/php-5.5.13/Zend/zend_variables.c(136) : Actual location (location was relayed) Last leak repeated 55 times === Total 56 memory leaks detected ===

So what's going on here is that when OPT_SCAN_RETRY is set, the actual scan logic happens in a loop, which sometimes comes back with zero entries when you're matching a pattern. The code isn't taking this into account and cleaning up memory when it occurs.

I'll get a hotfix up today to sort it.

Cheers,
Mike

michael-grunder added a commit that referenced this issue Sep 2, 2014
When you instruct phpredis to retry the scan command in the event
of a non zero iterator but zero elements being returned, it was
leaking memory, as it did not free the previous result.

Addresses #501
michael-grunder added a commit that referenced this issue Sep 2, 2014
When you instruct phpredis to retry the scan command in the event
of a non zero iterator but zero elements being returned, it was
leaking memory, as it did not free the previous result.

Addresses #501
@michael-grunder
Copy link
Member

Please try it now.

Cheers,
Mike

@aartua
Copy link
Author
aartua commented Sep 17, 2014

Thx, now it workin without memory leak.

@aartua aartua closed this as completed Sep 17, 2014
@lzj12530
Copy link

I have a problem with redis. i use php for hscan ,but i always get null. i don't know how it happend!
$test_key = "p:1222";
$redis->hMSet($test_key,['field'=>1,'field2'=>2,'field3'=>3]);
// $redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
$it = NULL;
$res = $redis->hScan($test_key,$it); // $res is null
thanks a lot! my environment is php 5.6 with redis 2.8 and the latest php extension redis.so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0