aspose file tools*
The moose likes Beginning Java and the fly likes how can i know size of object in memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can i know size of object in memory " Watch "how can i know size of object in memory " New topic
Author

how can i know size of object in memory

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello all

how can i know how much memory object is in memory

throw code or IDE ? or something

thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30506
    
150

Meir,
You can use a profiler and drill down to the object or write code. For code, write the object to a stream and see how many bytes it is.

With either approach, note that you might overcount the memory used. If another object shares objects owned by your app, they will be counted. Even though they may be shared by many objects.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11302
    
  16

one of the things about java is that you don't NEED to know. Java has built in memory management, so you don't need to calculate the size to find an offset.

I guess my question to you would be "Why do you want to know? What do you hope to gain by knowing, or do with the info?"

if it's purely an academic exercise, OK, but if you have some pressing design need, there's probably a better way.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
I'm with Fred on this one... given that this is the beginner's forum I would say that the first step would be to understand exactly how reference variables and objects interact, and to understand exactly when an object becomes eligible for garbage collection. From a memory management perspective, the single best thing you can do as a programmer is to understand how to make an object eligible for garbage collection.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've worked in other languages that had a sizeOf(object) feature and we thought we really needed it to tune designs. Years of working without it in Java shows that we really didn't need it for that purpose, though I'm still curious sometimes. The Java Specialists newsletter shows how to get a pretty good estimate. I've never actually tried it, because I can't really answer the questions Fred asked.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i know size of object in memory