Get the size of allocated objects

D

Davide

Hi to all !

Is it possible to get the size of RAM occupied by one or more java
objects ?

My problem is that I've a long-running thread that continue to allocate
RAM even if it shouldn't. I've debugged the code in order to understand
if some objects are cached or not correctly closed/finalized and I
forced the GC too, but a lot of memory remains occupied. I cannot
understand which are the objects that use so much memory so I'd like to
know if there are some tricks to get the allocated memory of existing
objects.

Thanks in advance.
Davide
 
A

Andrew Thompson

Davide said:
Is it possible to get the size of RAM occupied by one or more java
objects ?

My problem is that I've a long-running thread that continue to allocate
RAM even if it shouldn't.

Any memory leak is the real problem here, and that is what
you need to concentrate on. The actual size of objects will
(most probably) be seen to be irrelevant once that is fixed.
..I've debugged the code in order to understand
if some objects are cached or not correctly closed/finalized and I
forced the GC too,

No you haven't. You can suggest GC, but the VM will do it
when it is good and ready. If the VM determines a call to
GC is unnecessary, it will most likely ignore it entirely.
...but a lot of memory remains occupied.

You must be retaining references to objects somehow in your
code.. Speaking of which, do you have a short, compilable
example that fails?
<http://mindprod.com/jgloss/sscce.html>
 
D

Davide

Andrew said:
Any memory leak is the real problem here, and that is what
you need to concentrate on. The actual size of objects will
(most probably) be seen to be irrelevant once that is fixed.


No you haven't. You can suggest GC, but the VM will do it
when it is good and ready. If the VM determines a call to
GC is unnecessary, it will most likely ignore it entirely.


You must be retaining references to objects somehow in your
code.. Speaking of which, do you have a short, compilable
example that fails?
<http://mindprod.com/jgloss/sscce.html>

Unluckly, I use some external jars and, even if they're open source, I
would like to avoid the inspection of all that code.
Anyway, the "sscce approach" could be useful for me, at least to be
sure if the leaks are caused by my code or by those external codes.

Thank you
Davide
 
A

Andrew McDonagh

Davide said:
Andrew Thompson wrote:




Unluckly, I use some external jars and, even if they're open source, I
would like to avoid the inspection of all that code.
Anyway, the "sscce approach" could be useful for me, at least to be
sure if the leaks are caused by my code or by those external codes.

Thank you
Davide

one thing to keep in mind, typically when the GC does kick off (either
because current available memory has been exhausted or because of a
developer invoked 'suggestion' too), the GC will reclaim as much memory
as it can, *plus* pre-allocate another chuck of system ram for its heap.

For example....

1) current heap size is 80mb, available heap memory 0mb
2) OutOfMemoryException raised,
3) GC reclaims 10mb worth of objects
4) current heap size 100mb, available heap memory 30mb

Looking at the JVM's memory usage via the OS, will show it consuming
more memory, even though technically within the JVM, the application
itself is using less.
 
Z

zero

one thing to keep in mind, typically when the GC does kick off (either
because current available memory has been exhausted or because of a
developer invoked 'suggestion' too), the GC will reclaim as much memory
as it can, *plus* pre-allocate another chuck of system ram for its
heap.

For example....

1) current heap size is 80mb, available heap memory 0mb
2) OutOfMemoryException raised,
3) GC reclaims 10mb worth of objects
4) current heap size 100mb, available heap memory 30mb

Looking at the JVM's memory usage via the OS, will show it consuming
more memory, even though technically within the JVM, the application
itself is using less.

If the application no longer needs that 100mb, does the JVM release it
again before the application closes?
 
A

Alun Harford

Andrew Thompson said:
Any memory leak is the real problem here, and that is what
you need to concentrate on. The actual size of objects will
(most probably) be seen to be irrelevant once that is fixed.


No you haven't. You can suggest GC, but the VM will do it
when it is good and ready. If the VM determines a call to
GC is unnecessary, it will most likely ignore it entirely.

No it won't. From the javadoc on System.gc() and Runtime.gc():
"When control returns from the method call, the virtual machine has made its
best effort to recycle all discarded objects."
This doesn't guarantee that the garbage collector will have run (as there
may be no more objects to recycle - the garbage collector could have just
been run) but unless you do:

public void sillyMethod() {
System.gc();
System.gc(); //Safely ignored in a single-threaded environment
}

It almost certainly will run.

Alun Harford
 
A

Aray

Wow, jconsole seems full of magic, but the box I using is a win without
NTFS file system. can not learn it right now.

I will explore it latter while home.
 
C

Cos

Hi.

Well, you can consider using some of the latest and greatest tools
techniques bundled within Java5, I believe.

Here you can find more information about this
http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html

You'd be surprized how easy yet efficient is to do low-level memory
profiling and other stuff with these things.

On an other hand, if you're short of time consider to use latest
NetBeans5.0beta2 from
http://www.netbeans.org/downloads/index.html along with latest Profiler
bits. This new Profiler can do a lot of thing (including remote
profiling). It's all based on JVMTI layer, so you don't need to get
your hands dirty yourself :)

Take care,
Cos
http://weblogs.java.net/blog/cos/
 
J

Jim Janney

Davide said:
Hi to all !

Is it possible to get the size of RAM occupied by one or more java
objects ?

My problem is that I've a long-running thread that continue to allocate
RAM even if it shouldn't. I've debugged the code in order to understand
if some objects are cached or not correctly closed/finalized and I
forced the GC too, but a lot of memory remains occupied. I cannot
understand which are the objects that use so much memory so I'd like to
know if there are some tricks to get the allocated memory of existing
objects.

You might look at

http://www.javapractices.com/Topic83.cjp

"Here is a utility which can estimate the size of an object in
memory, if the object has a no-argument constructor. It follows
the style used by Java Platform Performance, by Wilson and
Kesselman."
 
O

Oliver Wong

My understanding is that it will only pre-allocate another chunk if it
feels like it needs to. Using your same example for step 1 and 2, at step 3,
let's pretend the GC instead reclaims 70mb worth of objects. Then step 4,
current heap size will remain 80mb, and available heap memory will be 70mb.
I don't know exactly what heuristic the JVM uses for deciding when to
allocate an extra chunk or not, but I'm pretty sure it's not every time a
garbage collection is done.
If the application no longer needs that 100mb, does the JVM release it
again before the application closes?

Yes, I believe so, based on watching the memory consumption bar in
Eclipse. Occasionally the heap size will shrink.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top