-3

How to do memory management in memory managed environment?

Is it just Garbage Collection or something else too?

In Java, GC is done periodically by VM without explicit call for garbage collection. I know that we can use System.gc() to request a garbage collection, but it is not guaranteed that garbage collection will take place.

So, is there any other way for memory management in memory managed environment?

Vikram
  • 3,996
  • 8
  • 37
  • 58

5 Answers5

2

There are no many options. You could additionally check how different types of GC are working http://sanjaal.com/java/574/java-general/all-about-java-garbage-collection-types-algorithms-advantages-and-disadvantages/ or try to tune GC parameters

user1459144
  • 4,439
  • 5
  • 28
  • 35
1

Garbage Collection is how memory is managed in a managed environment (in fact, that's why it is called a managed environment - because the memory is managed for the programmer).

In general, you don't need to manage memory in such environments.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • So, does it mean that in such environments programmer doesn't need to worry about memory at all? – Vikram Aug 01 '12 at 19:40
  • @Vikram - Well, you do need to worry about memory _consumption_, but not about allocating and deallocating memory for your objects. – Oded Aug 01 '12 at 19:41
  • @Oded: some people just don't trust that pesky GC. If you want something done, do it yourself. :) – Sergio Tulentsev Aug 01 '12 at 19:44
  • @SergioTulentsev - Well, having worked with some libraries that are supposed to make life easier, I know the feeling ;) – Oded Aug 01 '12 at 19:45
1

The short answer to your question is no. In a Java environment you really don't have any direct control over how heap space freed or managed. That being said, modern JVMs have extremely efficient GC algorithms and generally do a perfectly acceptably job of managing memory usage for all but the most extreme cases.

Even though you don't have any direct control over which objects get collected and when that process happens, you can use the reference classes from java.lang.ref to influence which objects are eligible and more likely to be garbage collected at a given time.

Mike Deck
  • 18,045
  • 16
  • 68
  • 92
0

Even though you can't really manage your memory at a technical level, developers are still able to manage memory to a certain degree. What I mean by this is that instead of trying to fight or improve the JVM, just let it do the work for you. But having said that, a poorly written program (i.e. bad memory management, aka. memory usage) can lead to a poor program experience.

But, if you manage your memory from a design perspective (i.e. not wasting memory, making use of appropriate algorithms and data structures, unreferencing/releasing memory as you see fit), these things allow you the developer some level of control.

AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
0

The short answer: You don't have to worry about garbage collection; the only thing you need to worry about is not to cause memory leaks in java. see this: Creating a memory leak with Java

Community
  • 1
  • 1
marcoo
  • 821
  • 1
  • 9
  • 25