memory usage by objects in common lisp

后端 未结 3 1904
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 01:15

Is there a way to find out how much memory is used by an instance of a class or basic data types in general?

I have a toy webframework in cl that creates and manage

3条回答
  •  攒了一身酷
    2021-01-18 01:54

    In Common Lisp the CLOS objects usually are a collection of slots. Typically these slots might be internally stored in some kind of vector. CLOS slots typically will contain either a pointer to some data object or, for a few primitive datatypes, may include the data itself. These primitive data types have to fit into a memory word: examples are fixnums and characters. Common Lisp implementations typically don't inline more complex data structures into a slot. For example a slot could be declared to contain a vector of fixnums. Implementations would not allocate this vector inside the CLOS object. The CLOS object will point to a vector object.

    The CLOS object itself should occupy then: number of slots * word size + overhead.

    Let's assume a word is 4 bytes long, 32bit.

    This might be the size for a CLOS object with ten slots:

    10 slots * 4 bytes + 8 bytes = 48 bytes
    

    Now imagine that each slot of a CLOS object points to a different string and each string is 100 bytes long.

    Example from above:

    1 CLOS object + 10 strings each 100 bytes.
    
    48 bytes + 10 * 100 = 1048 bytes
    

    Now imagine that each of the slot points to the same string:

    1 CLOS object + 1 string of 100 bytes.
    
    48 bytes + 100 bytes = 148 bytes
    

    To calculate the size of a CLOS object you could either:

    • just count the size of the CLOS object itself. That's easy.

    • somehow calculate a graph of objects with are reachable from the object, determine the unique memory objects (minus direct allocated primitive objects) and sum all memory sizes of those.

提交回复
热议问题