获取PHP中对象的引用计数? [英] Get the reference count of an object in PHP?

查看:88
本文介绍了获取PHP中对象的引用计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这个问题的反应是你不知道.",但请听我说.

I realize the knee-jerk response to this question is that "you dont.", but hear me out.

基本上,我在SQL上的活动记录系统上运行,为了防止同一数据库行出现重复的对象,我在工厂中为每个当前加载的对象保留了一个数组"(使用自动递增的"id"作为键).

Basically I am running on an active-record system on a SQL, and in order to prevent duplicate objects for the same database row I keep an 'array' in the factory with each currently loaded object (using an autoincrement 'id' as the key).

问题在于,当我尝试通过该系统通过该系统处理90,000多行时,PHP遇到了内存问题.通过每隔几百行运行一个垃圾收集很容易解决这个问题,但是不幸的是,由于工厂存储了每个对象的副本-PHP的垃圾收集不会释放任何这些节点.

The problem is that when I try to process 90,000+ rows through this system on the odd occasion, PHP hits memory issues. This would very easily be solved by running a garbage collect every few hundred rows, but unfortunately since the factory stores a copy of each object - PHP's garbage collection won't free any of these nodes.

我能想到的唯一解决方案是检查工厂中存储的对象的引用计数是否等于1(即没有任何东西引用该类),如果有,则释放它们.这可以解决我的问题,但是PHP没有引用计数方法? (除debug_zval_dump之外,但这几乎不可用).

The only solution I can think of, is to check if the reference count of the objects stored in the factory is equal to one (i.e. nothing is referencing that class), and if so free them. This would solve my issue, however PHP doesn't have a reference count method? (besides debug_zval_dump, but thats barely usable).

推荐答案

尽管debug_zval_dump和ob_start太丑陋了,无法包含在我的应用程序中,看来最好的答案仍然是获取引用计数.

It seems like the best answer was still getting the reference count, although debug_zval_dump and ob_start was too ugly a hack to include in my application.

相反,我使用refcount()函数编写了一个简单的PHP模块,该模块可在以下位置找到: http://github. com/qix/php_refcount

Instead I coded up a simple PHP module with a refcount() function, available at: http://github.com/qix/php_refcount

这篇关于获取PHP中对象的引用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆