×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: System.gc();

  1. #1
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    75

    System.gc();

    i discover that Nokia 6800 didnt run "System.gc()" or "Runtime.getRuntime().gc()".

    i have try this 2 type of code, but the real hp seem like didnt run this code at all.

    is there any other Nokia hp also have the same situation(didnt run System.gc())???

    seem like i have to "NOT" create any "OBJECT" at all

  2. #2
    Registered User
    Join Date
    Aug 2003
    Location
    London, UK
    Posts
    8
    What do you mean by this? System.gc() only suggests that the gc should run, it does not actually run it.

  3. #3
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    75
    ooo, is this mean that "System.gc()" will not run at real hand phone??? is it i have to "NOT" create "Many Object" to avoid "handphone reboot" problem??? other way that clear "unuse object" out???

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    14
    gc machamisms in J2ME and J2SE are different. in J2SE, there is a daemon thread to collect garbage, when System.gc() invoked, JVM forces the thread to run.
    But in J2ME, System.gc() collect useless memory immediately and return.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Location
    London, UK
    Posts
    8
    But in J2ME, System.gc() collect useless memory immediately and return.

    Where does it say this? The API docs says:

    Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    14
    I think we can think System.gc() as a simple routine to collect garbages.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Location
    London, UK
    Posts
    8
    To quote the docs once again
    Runtime.gc()(1):
    The Java Virtual Machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.

    You don't need to call System.gc(), as it will run when ever there is a pause in your application (waiting for input, IO bound operation, etc).


    1) This is the siemens docs, but that is only due the fact Sun does not host the API docs in any other format then html.zip and pdf, it is identical to that shipped with the WTK. (The NDS does not seam to ship with the API docs)

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    I want to know whether the "System.gc()" & "Runtime.getRuntime().gc()" are synchronous or asynchronous.

    Thanks.
    Last edited by momo_lu; 2003-08-05 at 14:52.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Location
    Tulsa, OK
    Posts
    17
    The documentation implies gc() is synchronous.

    "When control returns from the method call, the Java Virtual Machine has made its best effort to recycle all discarded objects."

    This quote says that the gc() call doesn't return until the JVM has done its collecting. Also, in my experience calling gc() can sometimes produce a little hiccup. So, likely synchronous.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    but i found it's not true.
    at least on j2me.

    are there any difference between "System.gc()" & "Runtime.gc()"

    thanks

  11. #11
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    75
    is this mean that we didnt need to code "System.gc()" or "Runtime.gc()" on the java program??? the handphone itself will run garbage collector when it need???

    my program on Nokia 6800 didnt run garbage collector at anytime, although i code "System.gc()" or "Runtime.gc()" on the program, it also didnt run garbage collector. any suggestion???

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    14
    to fabiolee:
    There are memory allocation and collection bugs in Nokia JVM,you can see some other posts in the forum.
    System.gc() does work, but when you create new object, JVM doesn't alloc the recycled memory,but request new memory from system.

  13. #13
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    System.gc() just calls Runtime.getRuntime().gc(), certainly in J2SE, and I see no reason why J2ME would be different. The JVM spec does not describe how or when the garbage collector works, just that it does(!)

    In terms of when - the garbage collector must ensure that you do not get out-of-memory exceptions when there is memory available. So it *must* run when there is insufficient memory to create a new object, until either there is enough memory, or until all garbage has been collected. Only when there is no garbage can an out-of-memory exception be thrown. You never *need* to run the gc, as it will be run for you. But it takes time; the gc() methods are there to allow you prompt the process into action when it suits you.

    The Nokia JVM does successfully recycle memory - run the Sun emulator and look at how much memory got allocated in total during the life of your program... if the gc didn't work your program would choke very quicky! On a series 40 (certainly 7210 and 6100, I think 6800 too) you are limited to 200k of heap and that's it. Symbian platforms are different - they can acquire extra heap space, and it is suggested that they may have some trouble giving it back... but any Java program that does much String manipulation (for example) would not survive for long even in 4Mb without successful garbage collection.

    Are you sure that garbage collection isn't running when you do System.gc()? I notice on the 7210 emulator that memory only goes down, and suddenly shoots up when it hits zero - but without any apparent garbage-collection delay. Maybe the freeMemory() isn't always accurate?

  14. #14
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    75
    ya, my Nokia 6800 hand phone didnt run "System.gc()" or "Runtime.getRuntime().gc()". i test it by "Runtime.getRuntime().freeMemory()" show on hand phone screen.

    on the emulator, the "free memory" will be maintain at a point, however at real 6800 hand phone, the "free memory" is dropping when it start running. "free memory" start at "110000", then it will "reboot" when "free memory" is about reach "70000".

    i really dunno y need "System.gc()" code??? y java didnt design as run garbage collector automatic when the object is no use.

    one more thing is that i dunno whether real hand phone will run garbage collector itself or wait for the "System.gc()" command to call garbage collector???

    anybody can answer my question???

  15. #15
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    The garbage collector DOES run automatically. You do not NEED to use System.gc().

    I have software running fine on a 6100 (series 40) without System.gc() at all... and creates lots of garbage, hundreds of k of it!

    If gc() followed by freeMemory() does not show any space being released, there are several possible reasons:

    i) there was no garbage; or
    ii) gc() doesn't cause garbage collection; or
    iii) freeMemory() doesn't give an accurate figure.

    I doubt that lack of memory is causing your reboot... 70k is still a lot.

    I will start a new thread about reboots... maybe someone else experiences these... so far it just seems to be you and me!!

    Graham.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×