Heap/Stack allocation

Hi!

i am a former c++ programmer trying to be more productive with java :wink:

But I really worry about the garbage collection. What I really miss is stack
allocation. I read about escape analysis, which would be a superior solution
to manual stack allocation - if it would work. I could not get it to work in a single
case.

I use a writing style like:

Matrix m = new Matrix();
m.translate(new Vector(0,0,0.1f));

and the translate method looks like:
public void translate(Vector d) {
x4+= d.x;
y4+= d.y;
z4+= d.z;
} // easy, huh ?

works fine, but everytime a Vektor is allocated on the heap. If I´d write in C++, I would allocate
the Vector on the stack, and everything is fine.

I checked this an other examples in the netbeans profiler, and i always see the “sawtooth” like curve
in the heap usage diagram. Heap memory gets allocated till a threshold is reached, and then freed.
I think this is horrible for realtime graphics. Why do I read so much about escape analysis, which is
my hope in this case - and it simply doesn´t work ?

Do I have to reuse the Vector every time ? That makes the code way more ugly.

Thanks a lot, if anybody could clear me up about this, I would really appreciate,
Frederick

Okay,

sorry I should have digged more into this forum Hope you don´t bother :wink:
Of course this is a well know problem…

And there seems to be no solution at the moment. Escape Analysis is not there
in jdk1.6 that was mentioned in some earlier posts. What I really dislike is
that there are lots of papers from Ibm and Sun that tell about cheap allocation
and Escape Analysis. When I read that, I thought wow is that ADVANCED -
really cool, and i was happy.
After actually trying out and finding that this cool features don´t work I am a bit
disappointed. :frowning:

Maybe I should just continue and don´t care about the stack, or go back to c++ cough
thanks,
Frederick

You are right this is an unsolved problem in Java 6… you might write an object pool if profiling shows that it’s a bottleneck, or you’re experiencing hickups at GC.

Chances are it’s not your bottleneck, and you shouldn’t worry about it. Otherwise, pool 'em!

It’s true that profligate use of “new” is inefficient, but you can
minimize the problem by not being profligate. In cases like
this, why not pass x,y,z as three arguments?

Of course I could pass as float, but this is not really acceptable for me,
because in my opinion a computer language is whole about abstraction,
and I really like it - so I want to use my vectors :slight_smile:

Maybe I will reuse Objects…
At least under Linux I have performance problems with my gl driver. Even
simple output, that means a rotating polygon, is stuttering every four seconds
or so. C++ as well as Java, has nothing to do with the GC.
Windows seems to be fine. Thats much more serious than the GC problem.

I think I will not worry about GC and hope for escape analysis to come :slight_smile:

This is one of those points that goes along with programming to the language you’re using (‘Thinking in Java’ and so on), or when people talk about ‘programming C++ in Java’. Every language has different performance characteristics that must be considered. It’s not necessarily premature optimization, but just standard rules of thumb. For instance, in C++ people often get bitten by copy constructors being called behind the scenes, so it’s a common idiom to mark them as ‘private’ when they aren’t needed or ‘explicit’ when they are.

So when performance is a concern, I think it’s important that you consider the common idioms for the language you use. For 99% of my Java code, I don’t think twice about allocating new objects. If it does turn out to be a performance bottleneck after profiling, then I’ll worry about it. But there are certainly cases where I do think twice about it. This would be one of them. If you look at the code for the JMonkey engine, you’ll see several places where they reuse a scratch Vector object in order to avoid allocation. That’s a good policy to follow with Vector operations.

http://www.javalobby.org/java/forums/t94239.html (related article)

This particular solution is not yet implemented… however for practical purposes garbage collection of short lived objects is pretty much a solved problem-- just using other mechanisms.

I suggest you code and not borrow trouble, then analyze, profile and tune.

Hey, thanks for the answers :slight_smile:

[quote]That’s a good policy to follow with Vector operations.
[/quote]
Okay thanks ! I will use this then.

This is the kind of articles which gives me bad headaches. I want to be confident
of the language I use, and don´t want to doubt it.
In my opinion Sun should really break compatibilty, add support for other languages,
implement templates proberbly, and maybe introduce value types or better escape
analysis. They could provide a “deprecated” hotspot compatibility mode running all
the all old code. I think engineers at sun could figure something out to make this changes
totally transparent to any user.
I want a flexible and modern language, in my opinion Java should be at the cutting edge
in contrast to be conservative all the time. Thats why I left C++ there is not much going on,
maybe it will support introspection in 10 years. 2009 Stroustrup will come up with the
design of the next gen C++, then another 6 years to implement it. So we will see C+0x, or
what its called 2015. I don´t want java to be like that.
I think Microsoft does a better job a this. They are really quirky at the moment, and they
have really cool graphics demos for C#, just look at “Rocket Commander” for example,
absolutely awesome. We need show off projects like that for java.
I want to use java, because its OpenSource and Sun is much nicer than Microsoft imho,
and its platform independent.

Okay i am writing to much, but articles like that do always give me headaches, which is bad,
i have to get some work done now :wink:
Frederick

Problem with C# is that it changes so fast no-one has the time to really learn how to use it. I’ve been doing Java for, hm, 10 years now, and I reckon I know about 10% of the APIs well and have just gotten my head around generics.

Anyway - don’t worry about allocation yet, just write the cleanest code you can and profile it later and optimise out the slow bits. Just like any other language.

Cas :slight_smile:

[quote] I think engineers at sun could figure something out to make this changes
totally transparent to any user.
[/quote]
You can do now too :slight_smile:
Java is opensource - so if things which seem important to you but not interesting enough to all others to implement it - you can do it yourself :slight_smile:

flexible != feature rich
modern == ??

1 Platform, Language changes with every release, no compatibility between them. Is this a good job?

A language has nothing to do with demos written in it. Have you had a look at arith or the Java2d/opengl samples?
Technically the java2d/opengl stuff is so awesome MS has nothing compareable.

We?

Well then use it, otherwise use .NET. Or sit down and work on the code.

Telling others what they should do that they success isn’t really the way of changing things.

lg Clemens

[quote]You can do now too :slight_smile:
[/quote]
:o Ohh noo, I am a poor CS Student, I can´t I am not a compiler engineer, and they won´t le me,
at least I hope so, this job is not for everyone.

[quote]flexible != feature rich
[/quote]
What I mean by flexible is reflection, dynamic class loading, dynamic compilation, easy combination
with script languages (groovy etc). Java has all this already, at least all I can think of a the moment :slight_smile:

Modern means thinking into the future, java was a real inventive language when it came up (GC and the
like), which is now getting more and more standard. Java should stay inventive, that´s what i wanted to say.
C# has learned from java´s past, why shouldn´t java also ?

[quote]1 Platform, Language changes with every release, no compatibility between them. Is this a good job?
[/quote]
Well, that´s the Microsoft way ;D
I think they are still testing out the language and experimenting, but in a few years when dust has settled, this
will look a little different, as usual with Microsoft.
I won´t say this is good practice, but sometimes some cuts have to be taken, just as in real life :wink:

[quote]A language has nothing to do with demos written in it. Have you had a look at arith or the Java2d/opengl samples?
Technically the java2d/opengl stuff is so awesome MS has nothing compareable.
[/quote]
Sadly that´s not true. Maybe the opengl/java2d interaction is superior to everything ManagedDX and XNA has
to offer. My first impression of jogl was horrible, because I saw a lightweight swing opengl canvas. The gears
example was so sluggish, that i wanted to run away. (Okay that´s fixed now, because of accelerated Java2D).
The both Java Games i like the most at the moment are Alien Flux (Hey Cas, thank´s for your advice :slight_smile: ) and
Tribal Trouble.
The fact is Microsoft cares a lot more about graphics and games, that´s the success of their platform, I think. Although
jogl/LWJGL is longer around, there are much more XNA/ManagedDX tutorials, even awesome Video tutorials, which
explain how to create a game like RocketCommander. Thats much smoother to start with. Although I wouldn´t recommend
XNA at the moment, because it is under heavy development, and you´re at the mery of microsoft. That´s the biggest downside.

These two alone are better than any tutorials for java I have seen, and theres much more.
http://www.rocketcommander.com/
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Starting_a_project.php

Sad but true.
Microsoft knows that they have to do the fun things to attract programmers.

[quote]We ?
[/quote]
As java is open source, it is more a community thing. What I mean by “we”, is the
open source community, which needs more attractive demos to catch developers, at least
that is what I think. Microsoft is very progressive at the moment, and I want java to be an
attractive alternative.

[quote]Telling others what they should do that they success isn’t really the way of changing things.
[/quote]
I am just telling my struggles. Its likely that others have them too. I am not telling you anything, I just tell
what I would like. I won´t do anything on that because this is way above my head. I just want java to be competitive
and performant, that´s all.

So I will sit down and work on code, but not on javas :wink:
Frederick

GC was hardly something invented with Java. My Commodore 64 used GC in it’s BASIC implementation in the early 80’s and it wasn’t new then either :slight_smile:

One thing Java managed to do was to really push virtual machines into the mainstream and really pushed the JVM to state of the art.

IMHO, Java is doing an ok job of keep things ‘inventive’ and pushing forward with great innovations. They of course don’t seem to do it as fast as we would like… but that has largely been a problem caused by the inevitable delays associated with the community process through which Java evolves. MS just does what they want and you can see from the results that they don’t put as much thought into it as the Java community does. This makes them a bit more responsive, but then nealy everything microsoft does has this huge suckage factor that goes along with it…

Didn’t the basic on C64 only support global variables? How do you GC that?

The GC was for collecting strings. (The only other variable types available were floats and ints).

Cas :slight_smile:

There is quite a lot of stuff written in Java, and I dont know of anyone complaining about a Vector in the memory or such.

Maybe when you come from C++ and are used to explicit memory handling, you are sensitive to the topic. But as others say I also say: Code first, complain later if there is really a problem. When I do Java a create Objects as I need them and never care. In most cases you wont notice any performance issues.

In the end it comes down to the 90/10 rule, 90% of the execution time is spent in 10% of the code. So if you game then runs on 100 fps, there may be some bottlenecks, and then you optimize just this lines of code, dont care for the rest. I wouldnt bother for 100 Vectors as long as I dont experience a slowdown or anything.

Maybe Java needs optimistic thinking. Hey, I’ll just new this, new that, it will probably work fine. I just start to think about it when code makes trouble. I had some problems with Java know and then, but never with Memory or Garbage Collection.

-JAW

Of course you are generally correct, but it can become a real problem in performance sensitive code, which is why these discussions come up every now and then.

Its always a tradeoff. When I have an array of size 100 and I access position 101, Java throws an Exception. When I did it in C, it worked, and I had problems tracing down the bug. C didnt care whatever I did in the memory with arrays or pointers or anything. Java is rather safe and makes it easy to find such errors.

So you give some control to the JVM, this has advantages and disadantages. Each language has its specific problems. You can at least combine Java with native code, so just write the important thing in C and call it from Java. I’d say Java is a tool and you can use it on some problems while there are better tools for other problems. Sometimes it goes down to optimisation in assembler.

I dont know C++ much, so I cannot compare. I always liked Java. I find errors quickly and it has a huge API with many stuff you need often.

-JAW