user435739
user435739

Reputation:

Is it possible to optionally disable garbage creator/collector in Java/Android?

If I know what memory I allocate and When it needs to be freed. Can I disable Garbage collection in Java?

I just want to make the memory requirement of my app <1mb.

I think there has to be an option to disable creating garbage and then collecting it. I don't want to keep garbage at all.

Moreover Garbage collection still leaves leaks, so in a regular C program finding leaks is tedious same is the case with Java GC.

Let me handle my garbage my self..

Upvotes: 1

Views: 3934

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533500

If I know what memory I allocate and When it needs to be freed. Can I disable Garbage collection in Java?

The garbage only triggers when you run out of memory (except the concurrent collectors) If you don't create much garbage and never run out of memory you won't need the GC and it won't run.

Why you would want to do this for an app, I don't know. This technique is used in a high frequency trading, to minimise or avoid the GC, but I don't see the point for an app.

I just want to make the memory requirement of my app <1mb.

This is very hard to do as the environment will use this much to run even a hello world program. Your application can add this much.

I think there has to be an option to disable creating garbage and then collecting it. I don't want to keep garbage at all.

If you don't want to keep garbage then you need a garbage collector. If you do the recycling yourself, you don't need a GC.

Moreover Garbage collection still leaves leaks, so in a regular C program finding leaks is tedious same is the case with Java GC.

The GC doesn't leak in the same way as C program does, memory can always be recovered. In Java a "memory leak" just means any undesirable increase in memory usage.

Let me handle my garbage my self..

No one is stopping you.

Upvotes: 2

Alexander
Alexander

Reputation: 48262

The closest thing to destructor you can do:

myObject = null;

System.gc();

But that's not advisable.

I would be more worried about some Java classes having a native peer the GC doesn't know nothing about. Such as you should always call Bitmap.recycle() when you do not need a bitmap anymore

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

No garbage collector is not in your control.

Its the job of the dalvik VM to free memory.

You can check this link

http://developer.android.com/training/articles/perf-tips.html

Check the topic under Avoid Creating Unnecessary Objects.

Also you should code in such a way that you don't run into memory leaks.

Even if its possible to disable the gc, why would you want to do that?(i am not aware of it and i don't think its possible to disable GC). The GC will free memory and if you disable it how will garbage collection take place.

Upvotes: 1

Related Questions