-1

I have a custom collection that stores compressed data.

How can I easily get the size in KB or MB of the collection?

I want to see how efficiently my data is being stored, and the effect of compression on some of the data.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • Unless you have a trivial data structure like an array of `byte[]` and you're not interested in slight overhead of Objects, take a memory dump and run some analyzer over it. – zapl Nov 21 '15 at 01:54
  • Are you storing the compressed data in byte array or in file system or in some custom object? – 11thdimension Nov 21 '15 at 02:03
  • @11thdimension I haven't gotten to the compression (zip, 7zip, etc?) part yet, but from my research on it it will be byte array since it will be in-memory. – Blankman Nov 21 '15 at 02:04
  • @Paul, asnwer accepted there does not give the actual size of memory but the size of reference variable, also it's questionable if the serialized object size is same as the object size me memory as suggested by others. However given the compressed nature of the data, memory size is probably already here if it's stored in byte array or stream. – 11thdimension Nov 21 '15 at 02:07
  • @Blankman , if you decide to store the data in byte array then length of the byte array itself would be the size in memory at least for the most relevant part to your program. Suppose before compression it takes a byte array of 1000 after compression it take say byte array of length 50. – 11thdimension Nov 21 '15 at 02:11

2 Answers2

0

Maybe the easiest way is to count the amount memory (object size) consumed when you add or remove items from your collection, at that time you might now the size of the compressed objects you put on your collection, just add that to the current size. I think this is an alternative from just calculating the whole size every time you need it.

To know the size of an individual object, you can use: http://www.javapractices.com/topic/TopicAction.do?Id=83

That technique basically gets the memory before you create an object and after you create it, with: Runtime.getRuntime().totalMemory()

There are other techniques.

Pablo Pazos
  • 3,080
  • 29
  • 42
-1

The quick and dirty answer would be:




    public static void main(final String[] args) {
            System.out.println("Before: " + getUsedMem());
            final byte[] bb = new byte[30 * 1024 * 1024]; // 30 MB
            System.out.println("After: " + getUsedMem());
        }

        private static String getUsedMem() {
            final long mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
            return (mem / 1024) + "KB";
        }

However, keep in mind that due to its garbage collector, multi-threading architecture etc., those numbers will not be precise. Especially if you run a GUI along with it. If you stick to a very simple program with no static initializers and no chance for the Garbage Collector to kick in, then you might get it pretty exact.

JayC667
  • 2,418
  • 2
  • 17
  • 31