-3

Possible Duplicates:
In Java, what is the best way to determine the size of an object?
sizeof java object

C has a sizeof operator, and it needs to have one, because the user has to manage calls to malloc, and because the size of primitive types (like long) is not standardized. But in java cant we find the sizeof an object? Also why java doesnot have sizeof operator or method?

Community
  • 1
  • 1
developer
  • 9,116
  • 29
  • 91
  • 150
  • 2
    Well, you can always `serialize()` it and check the length of the string it returns... – Kaivosukeltaja Apr 08 '11 at 05:58
  • its not the duplicate, im not asking how can i find the size of object. why java doesnot have sizeof operator or method to find size of object – developer Apr 08 '11 at 05:59
  • Are you asking "How does java manage memory"? – Shakakai Apr 08 '11 at 06:09
  • 3
    @Domodar: Why are you are copying questions!? This question was asked and answered here: [http://norvig.com/java-iaq.html](http://norvig.com/java-iaq.html) – hage Apr 08 '11 at 06:19

4 Answers4

12

You've kind of answered your own question, in c you manage memory in java the jvm does.

In c you're directly allocating the memory for the data structures you're storing (malloc) and so you often need to know the sizes of these things.

In java the memory system is largely abstracted away so you don't (usually) care you just call new and it'll do whatever it does and if different jvms do things differently the memory used by the classes you've declared may vary.

Sometimes it is useful to know how much memory your classes are taking up (say you're trying to reduce your memory footprint in a tightly constrained environment) but it's pretty rare that you'd need that sort of information at runtime.

Tom
  • 43,583
  • 4
  • 41
  • 61
  • 3
    Can you explain what is unclear to you? There is prenty of material on this subject, but I suspect there is a doubt you have but we cannot guess what it is. – Peter Lawrey Apr 08 '11 at 07:34
1

Also why java doesnot have sizeof operator or method?

Answer #1 - because Java doesn't need this. In C, the sizeof operator is needed so that you can malloc objects of the right size, for doing certain kinds of pointer arithmetic, and so on. In Java, you don't need to (and can't) do those kinds of thing.

Answer #2 - the size of an object in Java is rather rubbery, and indeed can change through the lifetime of the object (at least, in some JVMs). Ergo, a sizeof operator would be problematic.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

In c there is a lot of work with pointers. Programmer should manage memory by himself, so he should know the size of types. But in Java there is no manual memory management. JVM does all it, so no need in sizeof.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

In C you have to manually manage memory usage, you stated as much in your question. Since you need to allocated space for an object and remove it, the sizeof operator is required to do this quickly and easily. In Java, memory management is taken care of by the JVM. You don't need to manually allocate and de-allocate memory so the sizeof operator isn't necessary.

Shakakai
  • 3,514
  • 1
  • 16
  • 18