4

I had a discussion with my Computer Science teacher about how much memory a class in JAVA takes up. Neither of us knew a specific answer, but I had a few questions that he couldn't answer.

1) Is it as simple as each classes' amount of bytes that its primitive data types use is what determines its memory usage?

2) If you make an instance of a class and set it to null, would it still take up as many bytes as an instance that is not null?

3) Does a String with 100 characters in it, have the same amount of bytes as a single character string?

4) If a class has no variables, no methods, nothing, and just looks like this:

public class Test{}

would it still take up memory?

  • An instance, by definition, is not null. A variable can store a `null` reference, but an instance is the actual object. – Sotirios Delimanolis Jan 07 '14 at 00:30
  • i think there is room for the vm to decide, but i would think that every variable has the size of a reference (or less maybe for some primitives) and the reference points to an object (which is roughly the size of its primitives plus vtables, etc to support OOP). By that logic 1)no 2)no 3)a tiny bit more 4)yes the reference – user3125280 Jan 07 '14 at 00:34
  • 1
    Read pages 42-43 in [`Thinking in Java`](http://pervasive2.morselli.unimo.it/~nicola/courses/IngegneriaDelSoftware/java/ThinkingInJava.pdf) to learn where the information stored. If your teacher indeed can't answer these question s/he has no business teaching Java. – PM 77-1 Jan 07 '14 at 00:44
  • @PM77-1 I think that last comment is a little harsh. There's a considerable difference between compiler theory, which I believe this question aligns with, and computing language theory - probably the core skill of the teacher in question. You're also possibly assuming that the teacher is university level rather than, say, elementary school level - a big difference in core skill sets being required by either. – robnick Jan 07 '14 at 01:20
  • @robnick - I assume that this is High School teacher. And I expect anybody teaching Java to know words `stack` and `heap`, as well as understanding that a `class` itself (like almost everything in Java) is an `object`. – PM 77-1 Jan 07 '14 at 01:45
  • @PM77-1 Where are you reading those things, e.g. didn't understand the concept of a stack or heap? The core question is related to the inner workings of a Java compiler. And personally I doubt most High School teachers would have had the exposure to give detailed answers to those questions. But I'm sure they understand concepts you describe - but it certainly wasn't mentioned they didn't understand those same concepts. Hey peace out dude, I'm not wanting to cause an agro war :) – robnick Jan 07 '14 at 03:46

3 Answers3

2

You're confusing between two different things: the amount of memory that a class takes (meta-data, or "the blueprint" of the instances) and the amount of memory that an instance takes (data/object).

For classes you can use java.lang.instrument.Instrument.getObjectSize(yourObject.getClass()); and as for an instance - it doesn't necessarily has a fixed size. For example, if you have an instance of Person and it has a name field - then different names will capture different space according to the length of the String.

You can use profilers in order to see how much memory instances take, YourKit is an excellent profiler but it's not free.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

Use this: java.lang.instrument package

Compile that library and include in your jar. Then:

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static long getObjectSize(Object o) {
        return instrumentation.getObjectSize(o);
    }
}

In Manifest.MF add:

Premain-Class: ObjectSizeFetcher

Now do:

public class C {
    private int x;
    private int y;

    public static void main(String [] args) {
        System.out.println(ObjectSizeFetcher.getObjectSize(new Test()));
    }
}

You can invoke your code with:

java -javaagent:ObjectSizeFetcherAgent.jar C

source: In Java, what is the best way to determine the size of an object?

Community
  • 1
  • 1
72DFBF5B A0DF5BE9
  • 4,954
  • 3
  • 21
  • 24
0

You can measure the size using the technique here:

https://stackoverflow.com/a/383597/3049628

Beyond that you can usually guess the rough amount of memory (most things are either 4 bytes or 8 depending on in a 32 bit or 64 bit systems. Arrays are packed better.) but the actual implementation is JVM dependent so there is no fixed rule.

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128