## Memory Management

### Memory Management Reference

`ext:stack-overflow`

Stack overflow condition.

#### Class Precedence List

`ext:stack-overflow`, `storage-condition`, `serious-condition`, `condition`, `t`

#### Methods

        ext:stack-overflow-size
        condition
        integer

        ext:stack-overflow-type
        condition
        concept

`integer`

- A non-negative integer.

`concept`

- A symbol from table.memory.limits, except `ext:heap-size`.

##### Description

This condition is signaled when one of the stack limits in table.memory.limits are violated or dangerously approached. It can be handled by resetting the limits and continuing, or jumping to an outer control point.

`ext:storage-exhausted`

Memory overflow condition.

#### Class Precedence List

`ext:storage-exhausted`, `storage-condition`, `serious-condition`, `condition`, `t`

##### Description

This condition is signaled when &ECL; exhausts the `ext:heap-size` limit from table.memory.limits. In handling this condition &ECL; follows this logic

- If the heap size limit was set to 0 (that is no limit), but there is some free space in the safety region &ECL; frees this space and issues a non-restartable error. The user may jump to an outer point or quit.

- If the heap size had a finite limit, &ECL; offers the user the chance to resize it, issuing a restartable condition. The user may at this point use `(ext:set-limit 'ext:heap-size 0)` to remove the heap limit and avoid further messages, or use the `(continue)` restart to let &ECL; enlarge the heap by some amount.

- Independently of the heap size limit, if &ECL; finds that ther is no space to free or to grow, &ECL; simply quits. There will be no chance to do some cleanup because there is no way to cons any additional data.

`ext:get-finalizer`

Return finalizer associated to an object.

#### Function

        ext:get-finalizer
        object

`object`

- Any lisp object.

##### Description

This function returns the finalizer associated to an object, or &nil;.

`ext:get-limit`

Query a memory or stack limit.

#### Function

        ext:get-limit
        concept

`concept`

- A symbol.

##### Description

Queries the different memory and stack limits that condition &ECL;'s behavior. The value to be queried is denoted by the symbol `concept`, which should be one from the list table.memory.limits

`ext:set-finalizer`

Associate a finalizer to an object.

#### Function

        ext:set-finalizer
        object
        function

`object`

- Any lisp object.

`function`

- A function or closure that takes one argument or &nil;.

##### Description

If `function` is &nil; no finalizer is associated to the object. Otherwise `function` must be a function or a closure of one argument, which will be invoked before the object is destroyed.

##### Example

Close a file associated to an object.

    (defclass my-class () ((file :initarg :file :initform nil)))

    (defun finalize-my-class (x)
     (let ((s (slot-value x 'file)))
       (when s (format t "~%;;; Closing" s) (close s))))

    (defmethod initialize-instance :around ((my-instance my-class) &rest args)
      (ext:set-finalizer my-instance #'finalize-my-class)
      (call-next-method))

    (progn
      (make-instance 'my-class :file (open "~/.ecl.old" :direction :input))
      nil)

    (si::gc t)
    (si::gc t)

    ;; Closing

`ext:set-limit`

Set a memory or stack limit.

#### Function

        ext:set-limit
        concept
        value

`concept`

- A symbol.

`function`

- A positive integer.

##### Description

Changes the different memory and stack limits that condition &ECL;'s behavior. The value to be changed is denoted by the symbol `concept`, while the `value` is the new maximum size. The valid symbols and units are listed in table.memory.limits.

Note that the limit has to be positive, but it may be smaller than the previous value of the limit. However, if the supplied value is smaller than what &ECL; is using at the moment, the new value will be silently ignored.

##### Customizable memory limits

Concept | Units | Default | Command line
`ext:frame-stack` | Nested frames | 2048 | --frame-stack
`ext:binding-stack` | Bindings | 8192 |
`ext:c-stack` | Bytes | 128 kilobytes | --c-stack
`ext:heap-size` | Bytes | 256 megabytes | --heap-size
`ext:lisp-stack` | Bytes | 32 kilobytes | --lisp-stack