Does this code cause any memory problems?

Asked

Viewed 92 times

2

Follow the original code:

class Table{
    private static $table;

    public static function draw( stdData $data ){
        self::$table = new self;
        return self::$table->_draw( $data );
    }
    ...

I made this modification below to try to dodge a possible problem with memory:

class Table{
    private static $table;

    public static function draw( stdData $data ){
        self::$table = new self;
        $html = self::$table->_draw( $data );
        unset(self::$table);
        return $html;
    ...

But it caused trouble:

Fatal error: Attempt to unset static property Table::$table in Table.php on line 8

Does the original code cause any memory problems? What would be the correct way to get out of the problem?

That is, it occupies space in memory unnecessarily?

The goal is to use at another time echo Table::draw($data); to print a table and that after use have nothing in memory because of that.

  • What kind of memory problem are we talking about? I don’t know if this code makes any sense, since it doesn’t have a larger context.

  • I want to minimize memory usage as much as possible, in case free the memory that would be in self::$table. The class only generates a table html according to what is in $data.

  • you came to measure memory consumption?

2 answers

3


PHP is a language of script so programs run for a very short period. In general even if there are memory leaks is not usually very relevant.

The code doesn’t have much context but I’ll say it probably shouldn’t be like this. Normally the class should be normal and not static, if one is to be static even, perhaps it should not be a class. It seems too complicated for anything.

If you think the fact that the value is static is holding data in memory, the solution is simple, do not leave it static, so the moment the instance is no longer being used, it will be removed from memory.

You must be wondering why this class is static with global state or if this should be a class. There are even exceptions, but the basic rule is that you have been to avoid being static. Of course, if you know a lot what you are doing is no problem, it doesn’t seem to be the case. Go in simple.

I may be wrong, but this line self::$table = new self; doesn’t seem to make any sense.

If you want to insist on trying to reduce memory consumption assign a null to static member:

$table = null;

When you need to do this there’s something wrong with the code. I doubt I need this variable, the code doesn’t even show where it’s being used, maybe anywhere, and if it’s used the wrong way.

If the code is too confusing, there’s something wrong with it, this is the biggest problem. If you don’t understand 100% what’s going on in it, you’d better do it in a way you understand. Simplify. I’d help more if I had more context.

  • I do not understand what more context would be needed. In the 'normal' way at another time would make this use: $table = new Table();echo $table->draw($data); and would remain this $table occupying instance space unnecessarily (although minimal) until the end. But in the case I presented it would be possible to make this use echo Table::draw($data); and that I would like you to make no more use of memory of that instance created within this static function. I believe null already helps in wiping.

  • 1

    You’re too preoccupied with things that shouldn’t worry you. But mostly you’re trying to do "magic" to solve problems, which probably don’t even exist. And that might not change anything. Probably this whole class is wrong, but I only saw an excerpt of it. I hinted to assign a null. But almost every time you do this, you’re doing something wrong somewhere. I have serious doubts whether you should have this static variable. I told you what you need to do, if you don’t want to follow, feel free, the damage will be yours alone.

  • ... At no time do I put myself in "I don’t want to follow what you say". What I want is to understand your reason. I’m with you when releasing that memory is irrelevant and I’m looking for the egg hair. But I’d like to do it anyway, unless, as you say, there’s a problem. But it seems to me that the problem it says is 'just possibly in the rest', not in that shown, right? Being perfect in the remainder this shown is just unnecessary or something bad? Why?

  • My reason is to be pragmatic. I found it curious how your 3rd. sentence contradicts the 1st. : ) The biggest problem is in what I say in the last paragraph and brush in the previous ones. The whole code is weird. I will repeat, I can no longer help just by looking at this excerpt. Although helping more than this would no longer fit in this question. And I could only say as I would. But then you will say that you prefer to do it anyway. You are making an assumption that does not fit the reality. Programming is not about looking at a chunk of code and saying whether it’s right or wrong.

0

Just optimize the bottlenecks and not where you don’t need them. This ai code tries to 'destroy' a piece of the class. The correct one is to assign null on that property.

In place of:

unset(self::$table);

change to:

self::$table = null;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.