• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

memory taken by an object

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
can somebody tell me whether this program will give me the exact memory taken by the Object created.
import java.util.Vector;
class testmem
{
public static void main(String[] args)
{
Runtime rt = Runtime.getRuntime();
long start, end;
rt.gc();
start = rt.freeMemory();
Vector j = new Vector();
end = rt.freeMemory();
System.out.println("That took " + (start-end) + " bytes.");
}
}
If this program does not, can somebody tell me how we can find how much memory is taken by an object.
Thanks in advance
Shikhar
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shikhar,
According to the API freeMemory() returns an approximation of the amount of free memory available so your program should return an approximate value.
Not sure how you would find the exact value; especially as the API (under totalMemory() ) states that the amount of memory required to hold any object may be implementation dependent.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
shikhar singh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for reply..
but if you run my program, you will notice that the result is very consistent. According to my program ,everytime it shows that memory difference(memory taken by instance of vector) is 1376 bytes. Does this mean that Vector instance takes 1376 bytes for my JVM.
Also can somebody tell me ,approximately how much is the memory taken by a vector instance.
Thanks for the help so far
Shikhar
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shikhar,
Well, when I run your program I see '386 bytes' Which is why the number is approximate; it won't be the same on every system.
Vector sizes will depend on how many objects they reference. I don't think there is a preset size.
Just curious, why do you need to know the size? Java manages the memory for you so it shouldn't be a major concern.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic