4

I am working with Java 1.3, so it does not support generics. I came up with a shady work around. I want to know if the Integer and Double objects have an unnecessary overhead; I guess what I am asking is: do Integers take up same amount of space as an int? Same question for Double and doubles.

From what I know, the array of Objects holds an array of 32 bit integers that actually hold the address to the Object in memory independent of the array. But when I explicitly make an array of primitives like I did here, the outcome would be bad. Because, AFAIK, an array of primitives actually are an array of the data, not the pointers to the data. Or am I just assuming too much from C++?

So, if the Array in DataPackage actually does hold pointers instead of the primitives, I am good to go. But if they hold the primitives themselves, accessing the data will be a problem since a double is 64 bits of data, but a pointer to that is still 32 bits.

/**
 *
 * 
 * @todo Comment all the code.
 * @author Davidthefat
 * @version 1.0
 */
public class DataPackage {

    private String dataType;
    private Object data;

    /**
     * 
     * @param type
     * @param numOfItems
     */
    public DataPackage(String type, int numOfItems) {
        dataType = type;
        if (type.equals("Wheels")) {
            data = new int[numOfItems * 2];
        }
        if (type.equals("Arms")) {
            data = new int[numOfItems * 1];
        }
        if (type.equals("Joysticks")) {
            data = new double[numOfItems * 2];
        }
        if (type.equals("Buttons")) {
            data = new boolean[numOfItems * 4];
        }
    }

    /**
     * 
     * @param t1
     */
    public void update(Object t1) {
        data = t1;
    }

    /**
     * 
     * @return
     */
    public Object getData() {
        return data;
    }

    /**
     * 
     * @return
     */
    public String toString() {
        return "This contains " + dataType;
    }
}

To access that data, I cast the array of Objects as an array of Integers then call the intValue() function: temp is an array of ints. I must also put that getData in input just returns a DataPackage.

temp[0] = ((Integer[])input.getData("Wheels").getData())[0].intValue();

I can't just run this code right now since this is for the FRC Robot at my school, and school's out.

Alex K
  • 22,315
  • 19
  • 108
  • 236
Davidthefat
  • 63
  • 1
  • 8
  • 11
    If you're still working in Java 1.3 you've got bigger problems than if primitive wrappers add overhead. Oh, and welcome to SO! – Paul Dec 28 '11 at 21:18

5 Answers5

2

It's hard to follow your question but I can say this:

  1. An array of primitives int, double etc is as you suspect, just an array of the values, it's not pointers to the values.
  2. An Integer will take more memory than an int as an Integer is an object, so you will have the object overhead, where an int will take only the required space of the data. Same with Double etc.
Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
  • Yes, I have to articulate my question better. I first declare variable "data" as an array of Objects. I then initiate them as an array of ints. Later, in an another class, I access them as if they were Integers. Since there is that memory alignment issue, I should actually make the data an array of Integers instead of ints. – Davidthefat Dec 28 '11 at 21:40
  • I guess my big question here, is the memory consumption at this level really that important? Do you have an extremely small memory and a huge number of values such that saving a few bytes on each value matters? – Francis Upton IV Dec 28 '11 at 21:45
  • I was just worried about the mismatch in the sizes of the variables I was using. So if I store number as a 32bit int, then access it as a 44bit Integer, then I'll have problems because it will read more than needed and lead to mismatch in actual data. If you get what I am asking. So I just fixed that by making it all the wrapper objects instead of primitives. – Davidthefat Dec 28 '11 at 21:50
  • That sounds good. Both `Integer` and `int` hold the same amount of data (32 bits [signed]). Just make all of your numeric values `Long` and then you will not have to worry. – Francis Upton IV Dec 28 '11 at 21:56
  • 1
    @Davidthefat - I think you are mixing up the concept of pointers and objects. In C/C++ you can interchange between the integer type and a pointer, but you definitely **cannot** do the same thing in Java. If you declare an array of Object's in no way shape or form can you store primitive int's in it, and vice-versa. You have to choose whether you want to operate with Integer's or int's. – Perception Dec 28 '11 at 22:01
  • @Perception To nitpick: In an ANSI C/C++ program it's also not that simple to interchange integers and pointers (afaik the only integral type that's specified to be large enough to hold a pointer in C99/C++11 is optional in both implementations) ;) – Voo Dec 28 '11 at 23:15
1

You are assuming too much from C++. If you are worried about overhead here is an article about it.

To summarize the article an Integer has a 12 bytes overhead over a regular int; this is most likely because the JVM will try to align data. A Long is, however, the same size as an Integer proving this is an alignment issue.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
PhilippeLM
  • 163
  • 1
  • 1
  • 9
  • Note: The referenced paper is from 2002. – Andy Thomas Dec 28 '11 at 21:44
  • Are you sure its not 12 bits? 12 bytes is 96 bits. – Davidthefat Dec 28 '11 at 21:52
  • 2
    @ Andy, the original poster said he's working with Java 1.3; I supposed the reference paper is quite on target. – PhilippeLM Dec 28 '11 at 21:52
  • 2
    If the JDK version had anything to do with the overhead of objects, you'd be right. In reality though that depends solely on the used JVM. Which also reveals the next fatal flaw here: The overhead is implementation defined and may change at any time or between different JVMs. Still, for the interested: Hotspot has 2 words overhead per object and aligns on 8byte boundaries. – Voo Dec 28 '11 at 22:58
0

I'm only speculating but I imagine Integers take up a few times more space than just the int and pointer. I've heard the object itself has some overhead as well, which is increased with each method. I'd try running some tests on your system.

Zip184
  • 1,792
  • 2
  • 21
  • 34
0

As per this white paper, in java world you don't need to worry about overhead much, even though in terms of memory allocation Integer takes little higher than int type. http://java.sun.com/docs/white/langenv/Simple.doc1.html

kosa
  • 65,990
  • 13
  • 130
  • 167
  • 1
    You may indeed have to worry about overhead, depending on software requirements. Where in the referenced paper does it say differently? – Andy Thomas Dec 28 '11 at 21:43
  • Here it is, may not be exact words: The C notion of a pointer to an array of memory elements is gone, and with it, the arbitrary pointer arithmetic that leads to unreliable code in C. No longer can you walk off the end of an array, possibly trashing memory and leading to the famous "delayed-crash" syndrome, where a memory-access violation today manifests itself hours or days later. Programmers can be confident that array checking in Java will lead to more robust and reliable code. – kosa Dec 28 '11 at 21:44
  • I understand your concern in terms of overhead and agree with you. – kosa Dec 28 '11 at 21:50
  • 1
    All of that is true -- but it does not address the size difference between int and Integer, nor whether this merits concern. :) – Andy Thomas Dec 28 '11 at 21:53
  • @Andy Andecdotical evidence suggests that Java needs quite more memory than a comparable C++ application anyhow (the numbers often thrown around are ~2). So if someone is that memory constrained that he has to worry about the overhead of simple wrappers, chances are good Java won't be quite the right choice anyhow. Imo basically the last bastion for C++ programs: Severely memory constrained applications and low level stuff. – Voo Dec 28 '11 at 23:04
0

No, an int and an Integer does not take up the same amount of space. An int is 4 bytes, an Integer will be more than that (how much more can be measured, but it depends on if you are running in 32 or 64 bit mode). A common size seems to be 16 bytes for an Integer.

Not really sure what you want to accomplish, but your array will hold references to the objects if you use Integer, Double etc, but the values itself if you use int double etc. However, since Integer, Double etc are immutable I don't really know what you will gain by only having references to the objects.

And what system are you only that only supports Java 1.3? The EOL was initiated Oct 25, 2004 and ended December 11th, 2006, so it is quite aged.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53