1

I am using a lru cache which has limit on memory usage size. The lru cache include two data structures: a hashmap and a linked list. The hash map holds the cache objects and the linked list keeps records of cache object access order. In order to determine java object memory usage, I use an open source tool -- Classmexer agent which is a simple Java instrumentation agent at http://www.javamex.com/classmexer/.

I try another tool named SizeOf at http://sizeof.sourceforge.net/.

The problem is the performance very expensive. The time cost for one operation for measuring object size is around 1.3 sec which is very very slow. This can make the benefits of caching to be zero.

Is there any other solution to measuring a java object memory size when it is alive?

Thanks.

Mat
  • 202,337
  • 40
  • 393
  • 406
susanna
  • 1,395
  • 3
  • 20
  • 32
  • To get a size efficiently, you have to estimate its size. The fastest approach is to say the size is 1 and limit the number in the LRU cache. – Peter Lawrey Jan 23 '12 at 16:29
  • I want to the limits on memory size in bytes. So the number of cache objects is not what I want. – susanna Jan 23 '12 at 16:34
  • You can't do much about the cost of this. You should obviously cache class objects so you only do it once, but still. So the right solution probably is to use some WorkerThreads in the background for that computation.. – Voo Jan 23 '12 at 17:16

3 Answers3

1

Getting an accurate, to the byte value will be expensive as it needs to use reflection to get a nested size, but it can be done.

In Java, what is the best way to determine the size of an object?

http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html

Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    That's the one I use http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html?page=3 – SHiRKiT Jan 23 '12 at 16:58
0

Look at this: How to calculate the memory usage of a Java array Maybe it helps you in the analysis.

codejitsu
  • 3,162
  • 2
  • 24
  • 38
0

Since you already have a tool that does it...the reality is that there's no fast way to do this in Java.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413