struct equivalent (memory problems)

I am not aware of any Java equivalent to the C struct but I saw people suggesting that you just use a class instead. I tried that, but I think I am doing something wrong as it seems to eat up huge amounts of memory (4x).

The first program ran out of heap space when I gave the jvm 1500mb (-Xmx1536m). If i put in a counter, it was not even halfway through the for loop. So I wrote the second program which completed even if I only gave the JVM 700mb (-Xmx768m).

This doesn't seem equivalent to a struct. What am I doing wrong in my first program?

package core;

publicclass MemTest1{

privateclass Struct{

publicshort s;

publicint i;

publicbyte b;

}

public MemTest1(){

Struct[] r =new Struct[100 * 1000000];//700mb?

for(int i=0;i<100000000;i++){

r[i] =new Struct();

}

}

publicstaticvoid main(String[] args){

new MemTest1();

}

}

package core;

publicclass MemTest2{

privateshort[] s;

privateint[] i;

privatebyte[] b;

public MemTest2(){

s =newshort[100000000];

i =newint[100000000];

b =newbyte[100000000];

for(int j=0;j<100000000;j++){

s[j] = 1;

i[j] = 1;

b[j] = 1;

}

}

publicstaticvoid main(String[] args){

new MemTest2();

}

}

[3280 byte] By [00se7ena] at [2007-11-27 11:18:19]
# 1

By "equivalent to a C struct" you seem to mean "doesn't use any more memory than the component parts". Yeah, I think that Java objects aren't like that.

Stop trying to write C in Java.

paulcwa at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 2

Your problem isnt Java.

> b = new byte[100000000]; // byte = 1 byte

> s = new short[100000000]; // short = 2 bytes

> i = new int[100000000]; // int = 4 bytes

+ each object reference = 4 bytes

You can type equations and conversions into the Google search bar:

(100 000 000 * (1 + 2 + 4)) bytes = 667.572021 megabytes

((100 * 1 000 000) * (1 + 2 + 4 + 4)) * bytes = 1 049.04175 megabytes

The default available heap is 64 MB.

As for MemTest1 the problem could be allocating the -Xmx1536m.

Investigate this by using:

http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html

Runtime runtime = Runtime.getRuntime();

System.out.println("Max: " + runtime.maxMemory());

System.out.println("Free: " + runtime.freeMemory());

System.out.println("Total: " + runtime.totalMemory());

Report back with the results.

Maybe you should rethink using an intrepretted language

if you will be using 1 gigabyte of pure data alone and then complaining

about memory.

TuringPesta at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 3

By the way, you do HAVE 2 gigs of RAM, RIGHT?

TuringPesta at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 4

> Your problem isnt Java.

>

> > b = new byte[100000000]; // byte = 1 byte

> > s = new short[100000000]; // short = 2 bytes

> > i = new int[100000000]; // int = 4 bytes

>

> + each object reference = 4 bytes

Plus each Struct has 1 Object's worth of overhead. I think I saw somewhere that that's generally about 10-20 bytes, but I could be mistaken.

jverda at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 5

By "struct equivalent", I meant "the thing in Java that is most similar to a struct"

Yes, I have 2gb ram

Here is the result of adding runtime memory checking code:

Max: 1598226432

Free: 132901576

Total: 133234688

This should be enough by TuringPest's calculation, but it still runs out, so I think jverd is right:

> Plus each Struct has 1 Object's worth of overhead. I

> think I saw somewhere that that's generally about

> 10-20 bytes, but I could be mistaken.

This would make the most sense with the results I am getting.

So does anyone know why Java doesn't support structs? I find the language to have more standard features and be much more readalbe and well-documented than C/C++ so I like to use Java if I can.

00se7ena at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 6

> By "struct equivalent", I meant "the thing in Java

> that is most similar to a struct"

That would be a class with no methods (other than what it inherits from Object), and members that are all public and non-static, and that are primtives or other struct-like classes.

> This should be enough by TuringPest's calculation,

> but it still runs out, so I think jverd is right:

>

> > Plus each Struct has 1 Object's worth of overhead.

> I

> > think I saw somewhere that that's generally about

> > 10-20 bytes, but I could be mistaken.

>

> This would make the most sense with the results I am

> getting.

I'm definitely right about it having an Object's overhead. What I'm not sure about is how many bytes. You should be able to get a rough guesstimate by playing around with what you put into your class though.

> So does anyone know why Java doesn't support structs?

Because it tries to focus more on objects, and pure data containers aren't needed that often, and can be adequately simulated as described above when needed.

jverda at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 7

seven,

i recommend YourKit Java profiler. you can get a 30 day free trial.

it will show you how many bytes every Object takes up in memory.

like jverd said you can estimate this yourself.

the java classes themselves will take about 10(?) mb.

iterate through the for loop printing out a counter.

divide the memory by the number of iterations and subtract

the size of the data. this will give you the Object overhead for

future estimations. but it should be between 10-20 bytes like

mentioned.

what do you see as the difference between a struct and a class?

because the only difference is the 10 byte Object overhead.

if you want to store 1 gigabyte of pure data there is a solution.

Create a gigantic byte array. Then write a wrapper class over the

array for reading and writing shorts and ints.

This would be VERY simple and quick.

You just need to know your way around bitshifting.

Aside from any pedantic objections, it "works".

TuringPesta at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 8

Plus your Struct class is a non-static inner class of MemTest1 which means it also has a reference to a MemTest1 instance.

Try making the Struct class static, that isprivate static class Struct {

...

}

dwga at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 9

> Try making the Struct class static

Or top-level.

jverda at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 10

> By "struct equivalent", I meant "the thing in Java

> that is most similar to a struct"

That would be a class. As already pointed out, but nicely, it would be a very stupidly designed class.

> So does anyone know why Java doesn't support structs?

It does, inasmuch as you are allowed to create and use stupidly designed classes.

> I find the language to have more standard features

> and be much more readalbe and well-documented than

> C/C++ so I like to use Java if I can.

Then do so, but use Java to write Java code, not C code in Java. The behavior should be bundled with the data. The same applies to C++ for that matter.

By the way what is the need to create huge amounts of structs consisting of one byte, one short, and one int ?

paulcwa at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...
# 11

> So does anyone know why Java doesn't support structs?

> I find the language to have more standard features

> and be much more readalbe and well-documented than

> C/C++ so I like to use Java if I can.

Because structs aren't part of the language. The why's and wherefores of that are many, but the bottom line is, don't expect a feature in a language just because it's present in another. C doesn't support interfaces. Why not?

Structs aren't particularly object-oriented, to be honest. One of the things that makes a high-level language like Java more readable is the level of abstraction is closer to our own way of thinking, by design. Structs don't make much sense to human beings, objects do. The very thing you're missing is the very thing you're using Java to avoid :-)

georgemca at 2007-7-29 14:30:13 > top of Java-index,Java Essentials,Java Programming...