[Forcefully clean allocated memory]

I’ve had this method lying around for a few months now :smiley:

Might not be a smart idea, but I’ve implemented this to run every few seconds. :stuck_out_tongue:
The only noticeable difference is a 70FPS to 67FPS drop when it manages to clean something forcefully, and it reports back the megabyte’s of allocated memory cleaned.

(No need for pixel arrays / objects to be floating around, once they’re no longer needed execute this method and they’ll hit the road running, leaving you with some memory cleaned :))

Method:


	public static void fullGC() {
		final long MB = 1024;
		final Runtime rt = Runtime.getRuntime();
		long isFree = rt.freeMemory();
		long wasFree;
		do {
			wasFree = isFree;
			rt.gc();
			isFree = rt.freeMemory();
		} while (isFree > wasFree);
		rt.runFinalization();
		if (wasFree > isFree) {
			final long mbCleaned = Math.abs((isFree / MB / MB) - (wasFree / MB / MB));
			/* Only report back if we've cleaned > 1MB */
			if (mbCleaned > 0) {
				System.out.println("Managed to clean: "+mbCleaned+"mb.");
			}
		}
	}

Output:


Display Created.
(User re-sizes screen, pixel data increases)
(User restores screen to normal size, extra pixel data now floating around)
Managed to clean: 1mb.
Managed to clean: 2mb.
Managed to clean: 2mb.
Display Disposed.

Hope somebody finds this helpful, I’m use to hearing posts like “No, there’s no way to forcefully garbage collect…”. :point:

This sounds like a really, really terrible idea.

Serious WTF… System.gc() can cause “stop-the-world” garbage collection; doing it multiple times within a loop (let alone frequently within the game loop) might seriously affect performance and lead to a lot of stuttering/bugs.

General rule of thumb for desktop: as long as you aren’t doing anything stupid (like loading an image every frame), then you shouldn’t need to worry about GC and memory.

Couldn’t agree more, and this isn’t his opinion, it’s FACT.

If you’re the type of person who program’s a application and likes to just leave nulled / unused objects lying around consuming allocated memory sure, don’t worry about cleaning…

I on the other hand i do need all the memory free that I can use.

EDIT: Sorry I just ran over a method I haven’t seen in a while that contradicts a thousand statements saying “NO YOU CAN’T FORCE GC” and decided to post it on here :L

Yeah, their haters. Your method is brute force, it doesn’t stop the loop until SOMETHING is cleaned up… so of course it won’t return until something is cleaned up!

This isn’t “forcefully” doing jack, it’s waiting til gc eventually kicks in at some point and noticing the heap shrinking. If another thread does allocations, this one might never return. Now it’s true that Runtime.gc() will push a full gc well ahead of schedule (it’s what that little trash icon in eclipse calls) and I should mention again full gc. Thus defeating the purpose of a generational collector in the first place.

Basically this is just terrible advice, and anyone who actually knows enough to know when to call for full gc legitimately isn’t going to follow this nonsensical pattern.

lmao…
http://www.420magazine.com/forums/images/smilies/smokin2.gif

If I see a program in front of me that’s using 25MB of memory when it clearly only should be using 9MB (Just a example), if I can forcefully call ‘Garbage Collection’ when I choose too and get rid of unused / nulled objects lying around accumulating unnecessary space, of course I will. (Knocking it back down to 9MB)

“lmao” indeed. So basically either I’ve been trolled or you really are just being deliberately ignorant. Good show.

Like I said…

If you’re the type of person who program’s a application and likes to just leave nulled / unused objects lying around consuming allocated memory sure, don’t worry about cleaning…

If I can perform this with only 1 side effect being a ‘3 FPS’ drop on occasion… why not? :L

I’m the stupid one for doing more than your average programmer and optimizing memory?
lolz, mk skip.

EDIT: XD now your post says ‘deliberately ignorant’.

[quote]I’m the stupid one for doing more than your average programmer and optimizing memory?
[/quote]

It’s all good, hop off this thread if you don’t like it / don’t find it useful, don’t criticize me and literally call me stupid ROFL.

Pretty sure there’s some people out there who have browsed for this type of method and have found nothing but people saying “Nope, can’t force GC you can only ‘recommend it’…”.

15ms - 62ms elapsed for cleaning 4MB of memory, really a ‘studder / bug causer’ ey… :point:

http://cdn.uproxx.com/wp-content/uploads/2011/12/double-facepalm.jpg

Guys…I think he literally can’t see what he’s doing wrong…

Gabriel, I’m quite used to hearing you lash out about how “we’re all wrong and you’re right”. In fact, with a bit of digging, I can find that thread where you called me out for “criticizing everything you do”.

However, this time, please listen to us. This is useless. This does nothing. The GC will run on its own optimized time when the system/app/whatever needs the memory. Forcing it like this will do absolutely nothing other than make your application stutter and glitchy, not to mention that you still use Java2D, making matters worse.

Coupled with the side effects the others mentioned, plus the bug on line 13, this is a terrible idea and terrible advice. Take this not as insults, but as helpful and useful criticism!

It’s great that you are trying to help newbies by posting shared code; but you need to learn to take criticism. In this case, your code is utterly nonsensical and downright stupid. By suggesting this to newbies you are only encouraging bad programming practices and buggy code.

Your method doesn’t “force” GC. It only recommends it, and waits until GC has cleaned up some memory. Since System.gc isn’t guaranteed to do anything, your code is pretty much just an empty loop that blocks the game until memory is freed up at some point. There is no telling how long this wait may be, as it’s JVM dependent.

Further; if you are really concerned about a few extra bytes of memory, you shouldn’t be using Swing/AWT in the first place.

That’s funny, why don’t you go program a application that shows some live memory statistics. :emo: :emo:

Go accumulate some memory, see how long it takes to get cleaned. :yawn:

If you DO program a application that shows some live memory statistics, after you invoke this method you WILL see that the allocated memory has in fact ‘Deceased’.

So for you to say ‘This method doesn’t “Force” GC, it only recommends’ it is false, you’re just saying what the other 10,000 people have said :L

Aaaaand you’ve finally proved your ignorance and newbiness. I give up with you Gabriel. Have a good day!

It is pointless to keep responding to him guys. He doesn’t read nor does he understand.

I’m not going to argue with a 12 14 year old lol. :cranky:
gasp never mind, guess he’s apparently 17 now.

  1. I didn’t lash out, I said you were following most of rivens remarks up when they were aimed @ me but W.E.
  2. I never said or hinted ‘My works correct, all JGO’s is wrong’. ??? ???

Hey all let’s cool :slight_smile:

off-topic:
ra4king is the only 14 yo Homo Sp who I don’t want to mess up with in term of Java :persecutioncomplex:

@GabrielBailey74 these two links might help.


http://bugs.sun.com/view_bug.do?bug_id=6668279

I also like to focus on micro-optimizations but at the end of the day it is more important to focus on macro-optimizations and creating content before polishing it. (I’m guilty of calling System.gc() a couple of times in my game, I now know that it’s better not being there).

Thanks mate.

Googled: while (isFree > wasFree) and found out where I got that method from:
http://www.j2eeonline.com/advanced-java-oop/module1/forcing-garbage-collection.jsp

Thought it was a code snippet worth posting, W.E guess not.
I’ll delete it tomorrow if no one finds it helpful.
Thanks for all the reply’s,
http://www.420magazine.com/forums/images/smilies/smokin2.gif
apparently ra4king’s still butt hurt, pops up out of no where having not said a word to me in weeks/months with some ass backwards info.

Yes, I edited out “stupid”. Now I realize I shouldn’t have. In fact I have even stronger things to say but you are the furthest thing from being worth the effort.