• Post Reply Bookmark Topic Watch Topic
  • New Topic

Querying objects in memory  RSS feed

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how many registers in memory and memory it takes to store the variables value and _decimal (on the heap), how do I go about it?


 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't.
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.All the local variables are stored in stack in java and have their scope upto boundries of that block or method
All the primitive types have same size regardless of implementations of JVM in that system,like according to your example double would always be taking 64 bits(8 bytes) in memory,although I think it also depands upon memory management technique applied by JVM,means if JVM wants to store an int(32 bits) in 40 bits,it can.

2,All the objects are stored on heap but according to java 6 you can also make some objects on heap in java and references to them are stored according to creation of objects.If they are created locally(inside a block or method) then references would be stored in stack or on heap.
You can calculate the amount of memory taken by an object using Runtime class,like-

Runtime rt = Runtime.getRuntime();
long mem1 = rt.freeMemory();
System.out.println("total free memory is "+mem1);
Object o1=new Object();
long mem2 = rt.freeMemory();
System.out.println("Free memory is now: " + mem2);
System.out.println("Object size is: " + mem2-mem1
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no easy way to get the size of objects in memory in Java. Java is platform-independent and the JVM hides details like exactly how objects are represented in memory on the underlying platform.

There are ways to find out what the size of an object in memory is if you really want to know. Have a look at jvm-sizeof, for example, which uses the JVM instrumentation framework (a special API that the JVM has for external tools).

A double is a 64-bit double-precision floating-point number, and most likely takes up 64 bits (8 bytes) on Oracle's JVM for Windows, Linux and Mac OS X. How large a BigDecimal object is, is hard to say, but it is most likely a lot bigger than 8 bytes (and it depends on how many digits the number stored in the BigDecimal contains).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps a better answer from me would have been "You can, but it's probably not worth the effort. Why would you want to?"
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!