1

I am wondering what the options are for destroying a structure created via defstruct in Common Lisp. It appears that a constructor is automatically provided; however, that it is not the case for a destructor.

What are the ways one can "clear" a structure from memory? Things like (setq my-struct NIL) come to mind, but I am unsure whether this is the cleanest way to do this?

Edit:

The question arose when I was trying to test what happens if I keyed something into a hashtable using a structure as the key and then destroyed that structure. What would happen to the key in the hashtable? I guess this is more of a question on how hashtables are implemented.

8
  • 1
    Why do you care? GC will do it for you when you no longer use it. Commented Sep 17, 2018 at 19:50
  • 2
    Make sure you use local variables rather than global variables to hold temporary objects. Garbage collection will reclaim the memory when the variable's scope ends. Commented Sep 17, 2018 at 20:06
  • 1
    This is similar to JavaScript, PHP, Python, and Ruby. You don't have to manage memory manually, like you do in C or C++. Commented Sep 17, 2018 at 20:07
  • I understand all that. The question arose when I was trying to test what happens if I keyed something into a hashtable using a structure as the key and then destroyed that structure. What would happen to the key in the hashtable? I guess this is more of a question on how hashtables are implemented. Commented Sep 17, 2018 at 20:52
  • @MadPhysicist can you please add this comment as part of your question? Commented Sep 17, 2018 at 21:30

2 Answers 2

5

Since Common Lisp is a dynamic language, Garbage Collector will remove structure from memory when it's no longer in use (referenced anywhere). So yes, when you assign a name to a structure (ie my-struct) and then assign nil to this name, structure will be removed from memory.

When you use this structure also as a key, it has one reference more, so even when you assign nil to my-struct, structure will remain in memory until you remove it from the hash table.

It's worth noting, that make-hash-table takes also optional test argument:

test---a designator for one of the functions eq, eql, equal, or equalp. The default is eql. http://www.lispworks.com/documentation/HyperSpec/Body/f_mk_has.htm

When you use structures as keys, you should set test to equalp.

Sign up to request clarification or add additional context in comments.

2 Comments

When you use structures as keys, you should set test to equalp unless you want to compare by eq of course (which there are valid cases for)
Or until the hash-table is no longer reachable. Most lisp implementations will be able to clear up unreachable, but still (due to circular references, referenced) data. Some won't. Exact behaviour is seen as a quality-of-implementation thing.
4

There is no way for a user to 'clear' an object from memory. To free the memory and 'clear' it is the purpose of the Garbage Collector.

One may be able to use a non-standard mechanism of finalizers, which allows one to schedule actions when a garbage collector is about to destroy an object.

See 'finalize' in something like Trivial Garbage

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.