[Forcefully clean allocated memory]

In you opening post you seemed to be open to critique. When everybody around you says you made an obvious mistake, you’re either a genius or need to pay attention really quick.

LAWL, I didn’t make a mistake, this is a shared code snippet.
This method does exactly what the title says, you call it memory gets cleaned.
I built a application to monitor the allocated memory, there was no program blockage.

THIRD TIME:
Everywhere I’ve looked for methods on forcefully invoking garbage collection yield back the same result…
“No, you cannot forcefully call GC, you can only suggest it.”

This snippet is the opposite of it, somebody will make use of it / find it helpful, regardless of the 15ms delay. ;D
Feel like I’m on a forum with a bunch of blind people.
http://th279.photobucket.com/albums/kk143/faeini1/th_Smiley_blind.gif

http://th279.photobucket.com/albums/kk143/faeini1/th_Smiley_blind.gif

http://th279.photobucket.com/albums/kk143/faeini1/th_Smiley_blind.gif

Anyways, thanks for the tips all.

http://cdn.memegenerator.net/instances/250x250/22351068.jpg

Thanks to Mario and crew, I stay foolish enough to worry about memory or even delta but content only. :-*

There are six flaws: (apart from flaws already mentioned by others)

  1. if the jvm manages to shrink the heap, the free space might have reduced, after a GC.
  2. you will trigger full-GCs, which use a stop-the-world approach.
  3. continuously forcing the GC takes much more cpu time than letting the jvm determine the best time to run it.
  4. 70fps vs, 67fps might seem like a reasonable tradeoff, but you just froze the game for 40-50ms, which is noticable. Even a single framedrop at 60Hz can be noticed.
  5. in gaming, we happily trade memory usage for cpu cycles to keep framerate consistent.
  6. your method doesn’t really solve a problem, unless your OS is running out of RAM.

Thank you, duly noted.

(Probably why riven added #6 D:)
I’m sorry guys, I forgot to mention my computers a dinosaur… (I need RAM)
So I tend to over compensate in memory areas I guess? :persecutioncomplex:

Adding to that:

  1. since when is an MB equal to 1024?
  2. why did you use Math.abs(…) when calculating the amount of memory freed?

You can buy RAM for cheap. I can’t look in your wallet, but you may want to spend $10 on some second hand memory bank.

I don’t think this has been explicitly stated: on desktop/server hardware…virtual memory is pretty cheap and not something to think about unless you’re getting into a significant percentage of your physical memory.

And if you really what to play with how HotSpot does it’s GC thing…then way to go is through the literally hundreds of command line options.

I don’t know what you want to do, but considering my level I was gladly to use 1GHz 384MB memory machine which forced me to use JCreator rather Eclipse up to 1 years ago. Without wonderful features, I learned lot things about Java. Sometime limitation could bring gift.

In your case, machine limit brought you to think up a solution for memory optimization. You came up with one, posted it, and received lot of good advices.

A 50ms delay is completely unacceptable. It’s very easy to spot such a hickup and ruins any game even remotely based on timing (= all realtime games).

I think you’ve misunderstood 2 things.

First, using less memory doesn’t give more performance. It simply uses less memory, which if you have a veeery low amount of RAM is a good thing of course. However, if I remember correctly the VM cannot allocate more memory than is actually available, so as long as the program starts your computer won’t run out of memory (unless you also start something heavy in the background after).

Secondly, it’s impossible to get an out-of-memory error due to too much garbage. When you finally get an OutOfMemoryError, garbage collection will have already been run and the heap is actually 100% clean and filled with live objects.

Those two together should be enough to almost never make you manually call gc(), especially not in a time critical program like a game each frame. To be 100% honest, I do actually call gc() once in my code. I have a super-inefficient world loading method in one of my projects which sometimes allocates several gigabytes of shortlived data. The GC takes care of this perfectly well and there’s no significant performance loss there. After loading I SUGGEST that the GC should run a garbage collection to remove any remaining garbage after loading with gc(). That’s to prevent a large GC pause after the level starts to clean up the loading garbage.

Sorry if this has been mentioned, I kind of skipped over the drama.

I had this weird idea.

Let’s say that, for whatever reasontm (aka let’s not argue when this would be useful), your program needs to strictly manage memory.

Would it be interesting/worthwhile/not insane to design the program around an static (as in invariant, not as the Java meaning of static) memory allocation?

Meaning, you decide your program should only store up to 20Mb in memory, so at the start of the program you allocate said 20Mb, and only use your pre-allocated memory (specifics of how it would be allocated aside).

Similar to a Thread Pool. Could call it a Memory Pool, or Memory Buffer… Or You’re Doing It Wrong. :stuck_out_tongue:

Thinking about it, this could be useful in situations where data loading is unbounded, for example some sort of recursive loading loop, and you want to make sure it doesn’t grow out of control.

What a fun thread to start my day with. Reminds me of an old T-shirt slogan “Your village called, their idiot is missing” ;D

I’m intrigued by what the OP thinks that code is doing, too. Not seen if anyone else has mentioned this, but the other flaw with this would be that while this might release memory within the JVM, it’s extremely unlikely that the JVM will release that back to the OS - so in terms of keeping the memory usage of your program down it’s even more pointless.

Yes, let’s recreate the JVM using the JVM! :stuck_out_tongue:

Ah, but that is crazy like a fox. IBM’s research JVM (JikesRVM) is written in java and Oracle’s (potential) HotSpot replacement VM Maxine is 100% java.

Let me quote the Oracle Java documentation for System.gc():

[quote]Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
[/quote]
(from http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#gc())

According to spec: you “suggest” that the garbage collector “expend effort” towards garbage collection. Nowhere does it say that “suggest” implies “force”. Repeatedly suggesting something does not make it “forced”. If in your anecdotal case the VM happens to follow up on your suggestion (which it may have done anyways), that really does not say anything.

And also from Runtime.freeMemory():

[quote]Returns the amount of free memory in the Java Virtual Machine. Calling the gc method may result in increasing the value returned by freeMemory.
[/quote]
(from http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#freeMemory())

In other words, you’re releasing memory not to the system (i.e. for other applications), but to the JVM your app is running in. Memory that the GC would have made available to the JVM’s applications anytime they actually need it anyway. It’s wonderful what you can learn from just reading the manual.

In short: your approach does not help in any way, and just interferes with the highly optimized garbage collection approach taken by modern JVM’s. But I guess all of this has been said before on this thread ;D

System.gc does result in a gc fairly soon, unless there’s extraordinary circumstances preventing it. It’s still run asynchronously, with no completion notification available, so the method at the moment you call it is still a no-op, and the OP’s insistence that it guarantees anything falls flat in the face of any other thread doing allocation. And there’s the whole matter that it triggers an expensive full gc. Anyway, I think any newbies who run into this thread have already been duly warned.

Ok guys I think he and the rest of the world gets it. We’re just repeating each other now and most likely doing permanent psychological damage ;D

Lol. ;D

I’ll think twice before posting something next time lol.

I’m getting ready to post a snippet on ‘Manipulating Java2D into Java3D’, sound good?
Or should the criticism start in this thread lool

Misinformation is dangerous to newbies, so people on this forum will point out mistakes. In contrast to IRL, people actually feel okay being brutally honest with you thanks to the anonymity of the internet, so you have to be prepared to get criticism on what you post, especially if it’s something technical and does have a “right” or “wrong” (in contrast to more artistic work). Your post makes it sound like criticism is a bad thing.

Punctuating every sentence with “lol” doesn’t help. I can’t be the only one with that peeve.

tehe!

I’ll think thrice before thinking next time tehe!

tehe!