1

If I have a List<Object>, would it be possible to run some method on each Object to see how much memory each is consuming? I know nothing about each Object it may be an entire video file loaded onto the heap or just a two-byte string. I ultimately would like to know which objects to drop first before running out of memory.

I think Runtime.totalMemory() shows the memory currently used by the JVM, but I want to see the memory used by a single object.

trincot
  • 317,000
  • 35
  • 244
  • 286
User1
  • 39,458
  • 69
  • 187
  • 265
  • "I ultimately would like to know which objects to drop first before running out of memory.". Why? Sounds like a terrible idea. – akappa May 25 '11 at 23:39
  • maybe you should make some estimation/heuristics. objects you will place to the map will not come from nowhere. so, it should be possible to make some judgments about them. even if you will know exact size - it will not give too much. because you will get next question - which objects should be discarded? 1 large, or 10 small ones? – aav May 26 '11 at 00:01
  • 1
    but the very point is: why building software around implementation details which are explicitly undefined? Do you want to build a software which is exposed to implementation choices of the runtime, rather than on language semantics? – akappa May 26 '11 at 00:04
  • possible duplicate of [In Java, what is the best way to determine the size of an object?](http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object) – nes1983 May 26 '12 at 10:19

5 Answers5

1

SoftReference looks kinda like what you need. Create a list of soft references to your objects, and if those objects are not referenced anywhere and you run out of memory, JVM will delete some of them. I don't know how smart the algorithm for choosing what to delete is, but it could as well be removing those that will free most memory.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
0

If you are in a container you can use Jconsole http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

The jdk since 1.5 comes with heap dump ulits... You in a container or in eclipse? Also why do you have a List of Objects??

chrislovecnm
  • 2,549
  • 3
  • 20
  • 36
0

There is no clean way to do it. You can create a dummy OutputStream which will do nothing but counting number of bytes written. So, you can make some estimation about your object graph size by serializing it to such stream.

I would not advise to do it in production system. I, personally, did it once for experimenting and making estimations.

aav
  • 2,514
  • 1
  • 19
  • 27
  • sorry, but this is meaningless, unless you want very rough estimations. The serialized form of a thing and its representation in memory can be hugely different. Think, for example, to padding and memory alignment stuff, useful in memory but useless in serialized form. – akappa May 26 '11 at 00:01
  • as i *highlighted* it's just estimations. but for large chunks of data serialization overhead can be relatively small. – aav May 26 '11 at 00:03
  • and what about large amount of tiny objects? – akappa May 26 '11 at 00:05
  • absolute value, will be obviously wrong. relative - maybe not. – aav May 26 '11 at 00:09
0

Actually another possible tactic is just to make a crap load of instance of the class you want to check (like a million in an array).

The sheer number of objects should negate the overhead (as in the overhead of other stuff will be much smaller than your crap load of objects).

You will want to run this in isolation of course (ie public static main()).

I will admit you will need lots of memory for this test.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
-1

Something you could do is make a Map<Object, Long> which maps each object to it's memory size.

Then to measure the size of a particular object, you have to do it at instantiation of each object - measure the JVM memory use before (calling Runtime.totalMemory()) and after building the object (calling Runtime.totalMemory()) and take the difference between the two - that is the size of the object in memory. Then add the Object and Long to your map. From there you should be able to loop through all of the keys in the map and find the object using the largest amount of space.

I am not sure there is a way to do it per object after you already have your List<Object>... I hope this is helpful!

bamana
  • 1,625
  • 12
  • 15
  • this is just wrong: memory utilization is non-deterministic, because it is affected by garbage collections. – akappa May 25 '11 at 23:59
  • Good point. Could you force Garbage Collection before each measurement of `Runtime.totalMemory()` to get an approximation? I meant to reference this site: http://www.javapractices.com/topic/TopicAction.do?Id=83 – bamana May 26 '11 at 00:03
  • 1
    no. you cannot force GC. even if you call System.gc() - it's only a hint. also, actual garbage collection may run asynchronously. – aav May 26 '11 at 00:06
  • You guys are right, this isn't a valid solution... sorry. Thanks for clearing it up. – bamana May 26 '11 at 00:33