   Memory Usage
  I'm coding in java, and my memory used consistently increases until 21mb and then clears to 0mb and repeats. Obviously the garbage collector steps in at 21mb, but I want to get rid of this cyclic memory usage. What should I be looking for?EDIT: It speeds up with the frame rate, so its probably in the paint method. What kind of code should I look out for.   
  What's your code doing?You'll need to find out what's using up memory before you can do something about it.   ;  Object pooling. Instead of allocating memory to new objects and deallocating memory from old objects, consider pooling all objects you've finished with (eg: in a list) and then reissuing that object when your game/game engine requests a new one. It's particularly useful in things like particle systems where you can be allocating and deallocating a lot of objects.   ;  Its related to my frame rate, as you see in my edited first post. Should I post sauce?   ;Quote:Original post by beebs1What's your code doing?You'll need to find out what's using up memory before you can do something about it.how do i do this?   ;Quote:Original post by overeasyIts related to my frame rate, as you see in my edited first post. Should I post sauce?Posting some code would be helpful.   ;Quote:Original post by sybixsusObject pooling. Instead of allocating memory to new objects and deallocating memory from old objects, consider pooling all objects you've finished with (eg: in a list) and then reissuing that object when your game/game engine requests a new one. It's particularly useful in things like particle systems where you can be allocating and deallocating a lot of objects.Be careful applying something like this. Modern garbage collectors are optimised to deal with "typical" memory management. Object pooling can confuse the GC and, depending on other circumstances, decrease performance.An interesting discussion on this topic (although it is about C#, which has value types so not everything can be mapped to Java) is here.   ;Quote:Original post by overeasyI'm coding in java, and my memory used consistently increases until 21mb and then clears to 0mb and repeats. Obviously the garbage collector steps in at 21mb, but I want to get rid of this cyclic memory usage. What should I be looking for?This doesn't sound at all unusual. In particular, notice that you may not actually be using 21 MB of memory - rather it may be a few bytes each frame, which aren't collected until the GC runs short of memory.If you write a small test program that allocates and immediately orphans a lot of memory, you can notice that many Java implementations will never bother to collect the unused memory. This behaviour is typically desired, and not trivial to avoid.   ;Quote:I'm coding in java, and my memory used consistently increases until 21mb and then clears to 0mb and repeats. Obviously the garbage collector steps in at 21mb, but I want to get rid of this cyclic memory usage. What should I be looking for?This is how Java works. The only question that matters is - do these garbage collection cycles cause slowdowns?If so, then start your application with profiler:java -agentlib:hprof=heap=all,depth=10,doe=y,file=/profile.txt  com.yourclassThis will log all allocations made by your application. At the bottom, there is a list of allocation sites. Search for the trace number in same file, and it shows stack trace of where and how that part of code was called. Quote:Original post by rip-offBe careful applying something like this. Modern garbage collectors are optimised to deal with "typical" memory management. Object pooling can confuse the GC and, depending on other circumstances, decrease performance.Urban myth, IMHO. I've been hearing this same "modern garbage collectors" argument since 1.1.Quote:An interesting discussion on this topic (although it is about C#, which has value types so not everything can be mapped to Java) is here.Due to very lack of value types, object pooling (in Java) is a perfectly valid optimization method. Even java.lang.String does it with .intern().If developer knows that some algorithm will be constantly re-using a fixed number of objects, then pooling them will take the load off GC. But this should be one of less used optimizations, and only after memory profiler indicates such hot spots.   ;  If you didn't want a garbage handler, why did you choose Java?   