0

Is there a way to set a time limit on the GC?

I'm asking because I'm writing a game which allocates some floats, ints and longs on each event, and I'd rather call the GC for a short period of time (5-20ms) to remove those. I'd then call the GC in each Game Loop. This way would be my personal favourite as I still have some allocations which I can't backtrace without a lot of effort (I've already removed most stuff).

Alternatively I'd look for the other allocations and prevent them somehow, but then I'd need a way to prevent allocations in events.

Here's the result of allocation tracker if needed: http://textsave.de/?p=67220

EDIT: I can't really find anything with the allocation tracker, all the objects are from some far off java-internal class (even in the trace). Also, the shown objects are nowhere near the numbers being collected (unless the garbage collector counts float[] as multiple objects).

Anybody have this problem?

1 Answer 1

1

Is there a way to set a time limit on the GC?

No, but it is already fairly short.

I'm asking because I'm writing a game which allocates some floats, ints and longs on each event

Don't do that. Pre-allocate your memory.

and I'd rather call the GC for a short period of time (5-20ms) to remove those. I'd then call the GC in each Game Loop

You are welcome to call System.gc(), but you cannot control the duration for which it runs. It is usually on the order of low tens of milliseconds. However, you are better served by avoiding the allocations in the first place, by maintaining your own object pool.

Sign up to request clarification or add additional context in comments.

7 Comments

How do I do that? Pre-Allocate memory for an Object which is created in code which I can't edit? I've looked on google but I must be searching wrong.
@SBoss: "Pre-Allocate memory for an Object which is created in code which I can't edit?" -- then pre-allocate the object itself, if you can. Or, don't use this code in a game. Programming practices that are suitable for conventional Android applications are not necessarily suitable for game development. Also, bear in mind that on newer Android devices, garbage collection occurs in parallel on a background thread, so there are no "stop the world" pauses for it.
My target plattform is 2.1+, so I can't choose that. Do you know an alternative way to read in touch events? Somehow the onTouch(View,MotionEvent) call allocates quite a bit of stuff. So, all the objects I've seen so far are created in code behind. I'll just keep on trying to find out if it really isn't just me.
@SBoss: Well, games usually haven't had a problem with touch events and GC, AFAIK, though I am not a game expert. If you are trying to use real widgets here, versus doing your own drawing using the Canvas, I can see where you might run into issues like this.
Ok... I still can't see what I'm doing wrong. Do you know how to see which objects the GC collected?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.