Thread: what language should i use to

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by mmarab View Post
    Although you can play about with the garbage collector or force it,
    You can't force the GC, at best you can hint it (and doing so is usually counterproductive in that it forces the GC to (if it runs on the hint at all) perform a full GC cycle rather than an incremental GC cycle, consuming unnecessary CPU cycles, and can be forcibly ignored through JVM configuration).
    Just about the only time you may want to do that is when you have an application that you know will require a lot of memory for a longrunning operation. Just before consuming that memory you might want to try and get the GC to run so you have as much free RAM as possible, hopefully reducing GC overhead during that expensive operation.

    Quote Originally Posted by mmarab View Post
    Plus java is fun.
    It is, and productive as well.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by jwenting View Post
    You can't force the GC, at best you can hint it (and doing so is usually counterproductive in that it forces the GC to (if it runs on the hint at all) perform a full GC cycle rather than an incremental GC cycle, consuming unnecessary CPU cycles, and can be forcibly ignored through JVM configuration).
    Just about the only time you may want to do that is when you have an application that you know will require a lot of memory for a longrunning operation. Just before consuming that memory you might want to try and get the GC to run so you have as much free RAM as possible, hopefully reducing GC overhead during that expensive operation.
    Indeed. I have software I'm developing in Java that I wanted to take advantage of some.... -- perhaps I could call them "bizarre features" since they might be better implemented in a different way -- that require garbage collection. I find it annoying that I can't manage memory myself in Java, and I can only ask for the cleanup to possibly, maybe, perhaps, be attempted.

    If Java had a way to manually delete an object, that would be nice.

    I have to say, though, everytime I've asked the system to perform a garbage collection, it's appeared to work, and the necessary objects/classes appeared to be unloaded. I know this should not be relied upon, but I figure if it works... hey it works. If it doesn't, then it doesn't.

  3. #3
    Registered User mmarab's Avatar
    Join Date
    Jul 2007
    Posts
    30
    Example of kind of forcing garbage collection but using runtime: But i agree, using something like System.gc() is just a hint, and it does not guarantee immediate action, but actually it could slow your app down.

    Code:
    /*
    * This Java program shows you how to force garbage collection
    * Author: Megh Thakkar
    */
    public class CollectGarbage {
    
       int ASIZE = 1000000;
    
       void useMemory() {
    
          int[] intA = new int[ASIZE];
    
          for (int i=0; i<ASIZE; i++) {
            intA[i] = i*2;
          }
    
       }
    
       public static void main (String[] args) {
    
          CollectGarbage gct = new CollectGarbage();
    
          // Get a Runtime object
          Runtime r = Runtime.getRuntime();
    
          // Collect garbage at the start of the program
          r.gc();
    
          // Let's see how much memory we have at the start
          long availMem = r.freeMemory();
          System.out.println("At program start we have : " + availMem + " bytes");
    
          // Let's use some memory
          gct.useMemory();
    
          // Let's see how much memory is left
          long availMem1 = r.freeMemory();
          System.out.println("After running the program, 
    we have :  " + availMem1 + " bytes");
    
          // Collect garbage
          r.gc();
    
          //Let's see what we have now
          long availMem2 = r.freeMemory();
          System.out.println("After collecting garbage 
    we have :    " + availMem2 + " bytes");
    
          long freedMem = availMem2 - availMem1;
          System.out.println("Garbage collection 
    freed :    " + freedMem + " bytes");
    
    
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  2. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Language Script..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-30-2003, 06:48 AM