> 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>

Signature
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
Davide - 05 Jan 2006 12:55 GMT
> > Is it possible to get the size of RAM occupied by one or more java
> > objects ?
[quoted text clipped - 20 lines]
> 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
Andrew McDonagh - 05 Jan 2006 13:12 GMT
>>>Is it possible to get the size of RAM occupied by one or more java
>>>objects ?
[quoted text clipped - 28 lines]
> 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.
zero - 05 Jan 2006 13:30 GMT
Andrew McDonagh <news@andrewcdonagh.f2s.com> wrote in news:dpj609$qcg$1
@news.freedom2surf.net:
> 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
[quoted text clipped - 12 lines]
> 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?

Signature
Beware the False Authority Syndrome
Oliver Wong - 10 Jan 2006 17:26 GMT
> Andrew McDonagh <news@andrewcdonagh.f2s.com> wrote in news:dpj609$qcg$1
> @news.freedom2surf.net:
[quoted text clipped - 15 lines]
>> more memory, even though technically within the JVM, the application
>> itself is using less.
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
opalpa@gmail.com - 05 Jan 2006 13:31 GMT
http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html
The link above is for JConsole. JConsole will assist by providing a
rich profile of your application including much information about
memory consumption.
Have fun,
http://www.geocities.com/opalpaweb/
Aray - 06 Jan 2006 05:25 GMT
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.
<opalpa@gmail.com>
??????:1136467874.253031.149070@z14g2000cwz.googlegroups.com...
> http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html
>
[quoted text clipped - 4 lines]
> Have fun,
> http://www.geocities.com/opalpaweb/
Cos - 06 Jan 2006 21:27 GMT
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/
Alun Harford - 05 Jan 2006 14:16 GMT
>> Is it possible to get the size of RAM occupied by one or more java
>> objects ?
[quoted text clipped - 13 lines]
> 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
> Hi to all !
>
[quoted text clipped - 8 lines]
> 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."

Signature
Jim Janney