Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Mata: dropping structures from memory


From   "Zurab Sajaia" <[email protected]>
To   [email protected]
Subject   Re: st: Mata: dropping structures from memory
Date   Tue, 13 Jun 2006 13:51:12 -0400

Bill,

Thank you very much for your answer. My problem was slightly different though, so I'll try to put more details here:
I have one-dimensional linked list like x1.next->x2.next->...->xn.next... procedure examines existing members of the list and adds new members at the end. So after analyzed x1...xn added appropriate number of members to the list, I don't need x1..xn and want to (must) discard, because my list can accumulate millions of members if I dont (in fact I'm going trough nodes of a binary tree, which can be huge).

So I want to drop x1...xn but kepping xn+1... in the list ... and while writing this answer I realized I can drop members one at a time, analyze x1 then drop it and so on.

Now small question is how to clear one structure, assign null/missing values to each members? (-mata drop- doesn't work with structs as I understand)










From: [email protected] (William Gould, Stata)
Reply-To: [email protected]
To: [email protected]
Subject: Re: st: Mata: dropping structures from memory
Date: Tue, 13 Jun 2006 12:15:53 -0500

Zurab Sajaia <[email protected]> writes,

> I have a recursive problem involving linked list of structures in Mata, but
> after some number of chains (number is not fixed, that's why I decided to
> use list instead of a matrix) I don't need an earlier members of the list
> and they can be dropped to free some memory. If I don't drop them, my list
> is getting too long and even makes Stata to crash.
>
> I was wondering if there was some way of clearing structures like other
> objects in Stata.

I assume Zurab is using structure pointers. If not, I do not understand,
and Zurab will have to provide more information.

So let's imagine we have a structure like this:

struct node {
real scalar info
pointer(struct node) left
pointer(struct node) right
}

-struct node- is useful for creating binary trees.

So now imagine I'm inside a program, and variable -x- is a -struct node-.
Further assume that variable x is in the middle of the tree, so that
x.left != NULL and x.right != NULL.

Assume x.right points to a large branch of the tree. I now want to clear that
branch, and free all the nested structures. Just to be clear, *(x.right) is a
structure, and (x.right)->left might be another structure, and
(x.right)->right might be another, and if so, ((x.right)->left))->left might
be another structure, and ((x.right)->left))->right, and so on, and so on.

To chop of the tree at x.right, and free all memory associated with the
branch, I code

x.right = NULL

That is all there is no it!

Memory management is handled automatically by Mata. Mata will go through
the following logic:

I, Mata, have been asked to assign NULL to x.right. That is is
NULL is really not important, and I'll deal with that later.
First, however, I must deal with the fact that I have been asked
to assign something to x.right.

x.right might already contain something. I had better look.
Yes, I see it does. All right, let's drop and release the memory
associated with x.right.

Then Mata continues,

I, Mata, have been asked to release the memory of *(x.right).
*(x.right) is a structure, so to free it, I must free all the
elements of *(x.right). So I think I will tell myself to
free the first element of *(x.right), then free the second,
and so on.

Then Mata continues

I, mata, have been asked to free (*(x.right)).left.
I see that *(*(x.right).left) is a structure, so to free it, I must
free all the elements [...]

and Mata continues like that until there is no more -- until it runs into
pointers containing NULL.

I mention all of that because, you might think that coding

x.right = NULL

doesn't do very much except assign NULL to x.right. In fact, Mata is
contantly keeping track of what is in use and what isn't, and freeing
anything it can.

-- Bill
[email protected]
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index