• 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

how can i know size of object in memory

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all

how can i know how much memory object is in memory

throw code or IDE ? or something

thanks
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic