sizeof in Clojure

424 views
Skip to first unread message

Jan Rychter

unread,
Jul 21, 2009, 5:51:09 AM7/21/09
to clo...@googlegroups.com
Is there a way to get the size of a data structure in Clojure?

I've been wondering how much more space-efficient vectors are than
lists, but I don't know how to measure it.

--J.

Stuart Sierra

unread,
Jul 21, 2009, 10:07:46 AM7/21/09
to Clojure
Hi Jan,

Short answer: no, because Java has no sizeof operator.

You can use Java profiling tools to examine the memory usage of your
app if needed.

Since all of clojure's data structures are persistent (reusable), they
won't necessarily meet your expectations for memory usage based on the
standard definitions of vector/list/etc. A clojure vector is *not*
just an array. A clojure list is *not* just a linked list. The names
refer to the performance characteristics (vectors allow random access,
lists allow linear access) rather than the implementation. In fact,
most of Clojure's data structures are variants of trees, and they are
quite efficient.

If you are concerned about memory usage, I recommend you write a
prototype of your app using Clojure's built-in structures first. If
that uses too much memory, try refactoring your code to use Java
arrays or some other Java data structure optimized for your use case.

-SS

Daniel

unread,
Jul 21, 2009, 4:06:42 PM7/21/09
to clo...@googlegroups.com
On Tue, Jul 21, 2009 at 9:07 PM, Stuart
Sierra<the.stua...@gmail.com> wrote:
>
> Hi Jan,
>
> Short answer: no, because Java has no sizeof operator.
>
> You can use Java profiling tools to examine the memory usage of your
> app if needed.
>

I would recommend heading down the path that Stuart suggested. But
interestingly, there actually exists a 'solution' that doesn't rely on
a Profiler. The approach is described in a JavaWorld article
(http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html).
Keep in mind that it's 6 years old, and that it's written for Java,
and that the data structures for Clojure might have intricacies in
them, but with all that in mind, there are some gems in there if
you're set to have a sizeof operator in Clojure.

Have fun.

Daniel

Jan Rychter

unread,
Jul 24, 2009, 5:04:14 AM7/24/09
to clo...@googlegroups.com
> On Jul 21, 5:51 am, Jan Rychter <j...@rychter.com> wrote:
>> Is there a way to get the size of a data structure in Clojure?
>>
>> I've been wondering how much more space-efficient vectors are than
>> lists, but I don't know how to measure it.

Stuart Sierra <the.stua...@gmail.com> writes:
> Short answer: no, because Java has no sizeof operator.
>
> You can use Java profiling tools to examine the memory usage of your
> app if needed.
>
> Since all of clojure's data structures are persistent (reusable), they
> won't necessarily meet your expectations for memory usage based on the
> standard definitions of vector/list/etc. A clojure vector is *not*
> just an array. A clojure list is *not* just a linked list. The names
> refer to the performance characteristics (vectors allow random access,
> lists allow linear access) rather than the implementation. In fact,
> most of Clojure's data structures are variants of trees, and they are
> quite efficient.
>
> If you are concerned about memory usage, I recommend you write a
> prototype of your app using Clojure's built-in structures first. If
> that uses too much memory, try refactoring your code to use Java
> arrays or some other Java data structure optimized for your use case.

I should have described the problem better. In my case, I'm not
considering using native Java data structures. I have stacks implemented
on top of Clojure vectors and Clojure lists. I do not care that much
about random access, but I do care about O(1) count operation (which
both lists and vectors support). I measured the performance in a
real-life scenario and it is nearly identical for vectors and lists
(lists are actually slightly faster).

What I would like to know is given the same amount of data, which
structure will be more memory-efficient?

I realize this is not an obvious thing to measure, and I did look at the
article that describes a solution for Java
(http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html). I've
been wondering if something similar has been implemented for Clojure
data structures.

--J.

Rich Hickey

unread,
Jul 24, 2009, 7:39:36 AM7/24/09
to clo...@googlegroups.com

Vectors hold 32 items in a node while lists hold only one item per
node, so vectors will be more space efficient.

Rich

Reply all
Reply to author
Forward
0 new messages