Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Array Initial data gives code to large error

Reply
Thread Tools

Array Initial data gives code to large error

 
 
dcook@ccs-a.com
Guest
Posts: n/a
 
      02-27-2006
Hello,

Wondering if someone may have a solution for my problem with Initial
Data for object arrays. I have a 2D vector animator program that
auto-generates data for Java code. All the vector objects data is
output for a structures which I have converted to data classes. My
problem is the data can get too large for Java and I get a "code to
large" error from the compiler. So if the 2D project has something
like 5,000 box vectors in it, it outputs 4 coordinate values top, left,
bottom, right and a string for the box's name which is loaded into a
box class that is part of a box class array of 5,000. At first, I tried
something like this;

public class project_data {
public static final int box_count = 5,000;
public static box[] box_array = { new box(0,0,10,10,"box1"), new
box(20,20,40,40,"box2"), ....};
}

I couldn't have very many boxes in the array before I got a "code
to large" error so I then changed it to output the data as a string
array that I can then parse into data which increased the amount of
objects I can have. I created separated data classes for all data
types. I had to make them string arrays because it is mixed data.
Example;

public class box_data {
public static final String[] data = { "0,0,10,10,\"box1\"",
"20,20,40,40,\"box2\"", ...};

public static box[] box_data(int count) {
box[] dobj = new box[count];
for (int i = 0; i < count; i++) {
dobj = new box(data);
}
return dobj;
}
}

Class project_data {
public static final int box_count = 5,000;
public static box[] box_array = box_data(count);
}

Is there another way I can define the data to get past this limit in
Java or any suggestion of a better way so I can support more objects.

Thanks,

Dave

 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      02-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Wondering if someone may have a solution for my problem with Initial
> Data for object arrays. I have a 2D vector animator program that
> auto-generates data for Java code. All the vector objects data is
> output for a structures which I have converted to data classes. My
> problem is the data can get too large for Java and I get a "code to
> large" error from the compiler. So if the 2D project has something
> like 5,000 box vectors in it, it outputs 4 coordinate values top, left,
> bottom, right and a string for the box's name which is loaded into a
> box class that is part of a box class array of 5,000. At first, I tried
> something like this;
>
> public class project_data {
> public static final int box_count = 5,000;
> public static box[] box_array = { new box(0,0,10,10,"box1"), new
> box(20,20,40,40,"box2"), ....};
> }
>
> I couldn't have very many boxes in the array before I got a "code
> to large" error so I then changed it to output the data as a string
> array that I can then parse into data which increased the amount of
> objects I can have. I created separated data classes for all data
> types. I had to make them string arrays because it is mixed data.
> Example;
>
> public class box_data {
> public static final String[] data = { "0,0,10,10,\"box1\"",
> "20,20,40,40,\"box2\"", ...};
>
> public static box[] box_data(int count) {
> box[] dobj = new box[count];
> for (int i = 0; i < count; i++) {
> dobj = new box(data);
> }
> return dobj;
> }
> }
>
> Class project_data {
> public static final int box_count = 5,000;
> public static box[] box_array = box_data(count);
> }
>
> Is there another way I can define the data to get past this limit in
> Java or any suggestion of a better way so I can support more objects.


Write the object data to a file instead of generating Java code with it.
Have your program read the file at startup and create the necessary
objects. This will get around your code size problem very nicely, and
will also solve the problem of needing to have a new program for every
model.

--
John Bollinger
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
James McGill
Guest
Posts: n/a
 
      02-28-2006
On Mon, 2006-02-27 at 21:04 -0500, John C. Bollinger wrote:
>
> > public static final int box_count = 5,000;


Please tell me that's a syntax error and not some confounded
new feature.

 
Reply With Quote
 
James McGill
Guest
Posts: n/a
 
      02-28-2006
On Mon, 2006-02-27 at 13:05 -0800, (E-Mail Removed) wrote:


> Is there another way I can define the data to get past this limit in
> Java or any suggestion of a better way so I can support more objects.


Do you get further if you don't declare all your data "static?"

I'm guessing your JVM has to put all that static data into a "text"
segment. (how this is done, is largely up to the JVM implementation.)

You might be able to tune your JVM, if you really want your program to
work the way it's written. Or you might be able to put that data on the
heap, and be a lot less crowded.

What I take from this, is that there is apparently some hard limit on
class data, which means there's an effective limit on a class size.
I've never run into this or even considered it before.

I have to assume you know what you're doing. What's the rationale for
making all this data static, and does it still break if you make it
volatile?


James

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-28-2006
(E-Mail Removed) wrote:

> Is there another way I can define the data to get past this limit in
> Java or any suggestion of a better way so I can support more objects.


I second John's advice. I just wanted to mention that the reason for this
restriction is that initialisation expressions like the ones in your example
are compiled into code. (The resulting bytecode looks just like a long series
of assignments). There is a limit on the length of any method (which cannot be
circumvented since it's built into the classfile format), so it -- for some
unimaginable reason -- you /have/ to include the data in the code, then all you
can do is split the initialisation into several sub-methods.

-- chris


 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      02-28-2006
Chris Uppal wrote:
> (E-Mail Removed) wrote:
>
>> Is there another way I can define the data to get past this limit in
>> Java or any suggestion of a better way so I can support more objects.

>
> I second John's advice.


Yup. To get close to the generated code solution, I'd suggest using a
resource rather than a loose file, and probably serialisation.

> I just wanted to mention that the reason for this
> restriction is that initialisation expressions like the ones in your example
> are compiled into code. (The resulting bytecode looks just like a long series
> of assignments). There is a limit on the length of any method (which cannot be
> circumvented since it's built into the classfile format), so it -- for some
> unimaginable reason -- you /have/ to include the data in the code, then all you
> can do is split the initialisation into several sub-methods.


IIRC, you can exceed the limit. You just can't have debugging
information or try/catch/finally/synchronized blocks in the extended
area. Whether any particular compiler will generate such code is another
matter.

JSR202 was supposed to increase various size limits. However, it does
not appear to have done so. Perhaps huge JSPs from hell are out of fashion.

http://jcp.org/en/jsr/detail?id=202

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-28-2006
Thomas Hawtin wrote:

> IIRC, you can exceed the limit. You just can't have debugging
> information or try/catch/finally/synchronized blocks in the extended
> area. Whether any particular compiler will generate such code is another
> matter.


The compiler would also have to generate wide goto's to supplement the branch
instructions which only come in narrow flavours.

I thought it worth a giggle to try out with some hand-crafted bytecode.
HelloWorld with 1e6 NOPs before the System.out.println(). The resulting
classfile seems to be structurally OK (four different parsers manage to decode
it), but the JVM throws it out with an error:

Exception in thread "main" java.lang.ClassFormatError:
Invalid method Code length 1000009 in class file HelloWorld

Which is a pity, 'cos that would have been another solution for the OP --
generating bytecode directly instead of messing around with Java source

-- chris


 
Reply With Quote
 
dcook@ccs-a.com
Guest
Posts: n/a
 
      02-28-2006
John,

Thanks for the reply. This C++ animation app can output source in c or
Java to allow customer to modify and customize. The main customer base
is for c but see great potential for Java. Making the main C++ app
output Java formated data object files is not an option at this point.
Also, output all the data into a flat text file that I can parse into
Class data would increase to an unresonable Java app startup time.
There has to be a better solution.

Thanks,

Dave

 
Reply With Quote
 
dcook@ccs-a.com
Guest
Posts: n/a
 
      02-28-2006
James,

Yes, this was a syntax error. It would actualy be;

public static final int box_count = 5000;


Dave

 
Reply With Quote
 
dcook@ccs-a.com
Guest
Posts: n/a
 
      02-28-2006
James,

Thanks for the reply. Using the method of making the data a string
array allows me the largest data elements. I have tryed it as volatile
but was able to define larger amount of string arrays using static
final.

Thanks!

Dave

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Marshal gives error when dumping and loading array with two regexpsin latest ruby 1.9 Stefano Crocco Ruby 2 09-17-2009 08:21 AM
median of large data set (from large file) friend.05@gmail.com Perl Misc 5 04-02-2009 04:06 AM
Array#inject with hash as initial, unexpected error Matthew Moss Ruby 2 03-13-2006 03:59 AM
How do I do data binding with an ArrayList? Gives error. Alan Silver ASP .Net 4 07-11-2005 02:11 PM
[Urgent] Is there a size limit on returning a large dataset or a large typed array from web service? Ketchup ASP .Net Web Services 1 05-25-2004 10:11 AM



Advertisments