1

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..

  • There is no way to disable garbage collection entirely. see http://stackoverflow.com/questions/2927396/how-can-i-disable-java-garbage-collector – Bharat Gaikwad Jul 04 '13 at 06:28
  • With Java 11 you may use the EpsilonGC. See https://stackoverflow.com/questions/2927396/how-can-i-disable-java-garbage-collector – aventurin Sep 22 '18 at 17:54

3 Answers3

2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I agree on all points. Can you explain a bit more on this "If you do the recycling yourself, you don't need a GC.". Say i have a bitmap i call bitmap.recycle(). So you let the gc free the memory.http://developer.android.com/training/displaying-bitmaps/manage-memory.html. Before android 3.O you needed to call recycle on bitmap. – Raghunandan Jul 04 '13 at 06:44
  • 1
    @Raghunandan You can put the bitmap into a pool and if it is mutable, reuse it later. The GC doesn't get involved. – Peter Lawrey Jul 04 '13 at 07:03
  • Can we put everything in a Pool and free the pool later. In Objective-C there is NSAutoReleasePool. Is there a same thing here. Say I want to put everything related to the activity in a pool in onCreate and free the pool on onDestroy. Is it possible? –  Jul 04 '13 at 07:24
  • @Manish it is possible, and I have do this sort of work in HFT all the time. I still don't see the point for an App. What would happen if you didn't do this and let the GC clean up for you? For example in HFT I can say it will cost $X,000 each time we GC. What is the cost in your case? – Peter Lawrey Jul 04 '13 at 07:30
  • I am writing an app that should load very fast even when the system is heavily loaded. The app runs a service in background (30secs timer in it). Now If my app gets bloated and waiting to do GC, the whole point of the app being launched in a low memory situation is no achieved. Thats the reason we are trying to create a app which is <1mb or <2mb in size. –  Jul 04 '13 at 07:34
  • @Manish under what situations does your app gets bloated http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html. I agree with peter you should just trust the GC. – Raghunandan Jul 04 '13 at 08:12
  • Java most definitely leaks memory in the same way as C does. Memory leaks are one of the most common reasons Android apps crash. As for why you'd want to stop GC- well I just wrote an app that added 10 million records to a db for perf testing. Doing it in bunches and only GCing between bunches quadrupled performance. A game could be utterly ruined by GC going off at the wrong time. The "let the magic GC handle it" mentality only works when your app has no performance bottlenecks. – Gabe Sechan Mar 01 '17 at 18:14
  • @GabeSechan we actually take a more extreme case and make sure the garbage produced in a day is less than the size of the eden space, even if you are processing millions of messages. ;) – Peter Lawrey Mar 01 '17 at 18:17
1

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.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • And dalvik is no where good at it. i think it is a bad piece of code. –  Jul 04 '13 at 05:34
  • its more about who the hell is taking so much memory. Can you write a simple hello world program and check how much memory it uses. It is absurd 4-6mb –  Jul 04 '13 at 05:36
  • 2
    @Manish: if you don't like the GC, you *can* write native code in Android just fine: https://developer.android.com/tools/sdk/ndk/index.html – Joachim Sauer Jul 04 '13 at 05:38
  • Can i program UI elements in native code. Also I am just begging to save memory (I have no issues with GC as long as it let me work the way I want) –  Jul 04 '13 at 05:38
  • @Manish take a look at http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html. Also why would you want to disable GC? – Raghunandan Jul 04 '13 at 05:39
  • I want to clean up the memory as I exit from a function. Is it not a good design practise. –  Jul 04 '13 at 05:41
  • 2
    @Manish i would suggest you stop worrying about it and let the GC do the job. – Raghunandan Jul 04 '13 at 05:42
  • 2
    @Manish there are various factors that determine the size of your app. Like images. Images occupy more space. You can use proguard in release mode this will shrink and obfuscate your code. There many other factors to be considered. So without knowing what your app looks like i can't comment further. – Raghunandan Jul 04 '13 at 05:54
  • i urge you to create a simple hello world application and check its size (memory RAM) –  Jul 04 '13 at 05:55
  • don't know why this is downvoted. please leave a comment to describe what is wrong in the answer. – Raghunandan Jul 04 '13 at 06:00
0

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

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158