Ilion
Ilion

Reputation: 1

Memory usage change very fast, is it normal?

I just wonder, is it normal that memory usage is changing so fast.

I run this code It is tutorial 'How to make a simple game.'

I add some code in main loop of the game:

g.setColor(Color.cyan);
g.drawString(System.currentTimeMillis() + "\n\n/nRAM:[" + (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024 + " MB]", 100, 100);

The RAM usage is changing very fast between 25-30 and so on,

My question is why is it happens ? Is it wrong behavior or it doesnt matter ? Is the code wrong written ?

EDIT: Im suspicious because for example on this video memory usage is stable, moreover the brackeen game have same usage (25mb) until the sound stop's playing.

Upvotes: 0

Views: 99

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533810

If you create lots of garbage, you can see it producing 500 MB per second or more.

If you use a memory profiler you can reduce memory consumption and it can drop to less than 1 MB per minute for a running program.

Upvotes: 1

esaj
esaj

Reputation: 16035

In Java, memory management is handled automatically by a garbage collector. So, once an object is no longer needed (there are no references left to it), it doesn't get free'd right away, but instead stays in the memory. Every now and then (depending on several factors, like JVM settings, maximum heap sizes etc.), the garbage collection kicks in and removes the unneeded objects. Since this doesn't happen every frame, you see the memory usage climbing, and then drop suddenly as the garbage collection has occurred.

Upvotes: 3

Related Questions