pecl-dbus icon indicating copy to clipboard operation
pecl-dbus copied to clipboard

Memory leaks during data retrieval

Open msaladna opened this issue 2 years ago • 1 comments

Hello,

I'm running into memory leaks over repeated use of pecl-dbus during data retrieval from the bus. I've experimented with OPCache disabled as well to no avail.

Sample code used in the example:

<?php
			gc_collect_cycles();
			gc_mem_caches();
			$memory = memory_get_usage();
			for ($i = 0; $i < 10;) {
				gc_collect_cycles();
				gc_mem_caches();
				(new \Dbus(\Dbus::BUS_SYSTEM))->createProxy('org.freedesktop.systemd1', '/org/freedesktop/systemd1',
					'org.freedesktop.systemd1.Manager')->ListUnits()->getData();
				printf("itr: %d usage: %.2f KB\n", ++$i, (memory_get_usage()-$memory)/1024);
			}

Calling Dbus::createProxy by itself results in minimal memory overhead,

itr: 1 usage: 0.16 KB
itr: 2 usage: 0.36 KB
itr: 3 usage: 0.52 KB
itr: 4 usage: 0.69 KB
itr: 5 usage: 0.85 KB
itr: 6 usage: 1.02 KB
itr: 7 usage: 1.18 KB
itr: 8 usage: 1.34 KB
itr: 9 usage: 1.51 KB
itr: 10 usage: 1.67 KB

Calling an interface method starts leaking via DBus retrieval in ListUnits():

itr: 1 usage: 2293.08 KB
itr: 2 usage: 4594.19 KB
itr: 3 usage: 6831.27 KB
itr: 4 usage: 9196.34 KB
itr: 5 usage: 11433.42 KB
itr: 6 usage: 13670.50 KB
itr: 7 usage: 15907.58 KB
itr: 8 usage: 18400.66 KB
itr: 9 usage: 20637.73 KB
itr: 10 usage: 22874.81 KB

Then data transformation in getData() worsening memory usage in this example:

itr: 1 usage: 2293.08 KB
itr: 2 usage: 6425.97 KB
itr: 3 usage: 10494.83 KB
itr: 4 usage: 14691.69 KB
itr: 5 usage: 18760.55 KB
itr: 6 usage: 22829.41 KB
itr: 7 usage: 26898.27 KB
itr: 8 usage: 31223.12 KB
itr: 9 usage: 35291.98 KB
itr: 10 usage: 39360.84 KB

I've benchmarked against a simpler query, GetUnit('httpd.service')->getData(). Memory growth each query was the same between GetUnit() and getData(), ~0.25 KB.

Here's one more sample against org.freedesktop.systemd1.Unit interface:

<?php
			gc_collect_cycles();
			gc_mem_caches();
			$memory = memory_get_usage();
			for ($i = 0; $i < 10;) {
				gc_collect_cycles();
				gc_mem_caches();
				(new \Dbus(\Dbus::BUS_SYSTEM))->createProxy('org.freedesktop.systemd1', '/org/freedesktop/systemd1/unit/httpd_2eservice',
					'org.freedesktop.DBus.Properties')->GetAll(
					'org.freedesktop.systemd1.Unit')->getData();
				printf("itr: %d usage: %.2f KB\n", ++$i, (memory_get_usage()-$memory)/1024);
			}

With and without getData() transformation:

itr: 1 usage: 41.39 KB
itr: 2 usage: 82.81 KB
itr: 3 usage: 124.20 KB
itr: 4 usage: 165.59 KB
itr: 5 usage: 206.98 KB
itr: 6 usage: 256.38 KB
itr: 7 usage: 297.77 KB
itr: 8 usage: 339.16 KB
itr: 9 usage: 380.55 KB
itr: 10 usage: 421.94 KB
itr: 1 usage: 41.39 KB
itr: 2 usage: 124.12 KB
itr: 3 usage: 206.81 KB
itr: 4 usage: 289.51 KB
itr: 5 usage: 372.20 KB
itr: 6 usage: 462.90 KB
itr: 7 usage: 545.59 KB
itr: 8 usage: 628.29 KB
itr: 9 usage: 710.98 KB
itr: 10 usage: 793.68 KB

msaladna avatar Dec 30 '22 19:12 msaladna

Hi, do you have any news about this issue? I notice the same behaviour.

davideandreuzza avatar Sep 18 '23 08:09 davideandreuzza