Understanding Garbage Collection

Garbase Collector collects any objects that the program cannot access. If an object cannot be accessed, it will be collected by the garbage collector if needed.
Can someone explain with a simple example when an object cannot be garbase collected? preferably an example with 'String'
thanks

String text = "Hallo";
//the instance "Hallo" cannot be collected here
String message = text;
text = null;
//neither here;
message = null;
//The instance "Hallo" can be collecter here by the GBI don't think that this is the way it works. Garbage collection frees memory for use, and in this case, the variable text could still be referenced, even though it does not have a value. I might be wrong, but my guess would be that setting a variable to be null, would result in the location of memory allocated for the variable to be emptied (e.g. C-function memset). If the garbage collection would occur when the variable is set to null might cause problems, like so:
String foo = "bar; // Create and initliaze variable
foo = null; // Set to null, assume garbage collection
String bar = "foor"; // Create and initialize. If this were now stored
// in the same memory slot as foo, which would be possible due
// to the garbage collection, problems would arise from next line
foo = "bar"; // Now foo and bar refer to the same memory locationTherefore garbage collection occurs only when no object or class holds no reference to the variable, example:
if(foobar) {
   String localVar = "local variable";
// When the if-block ends, the variable localVariable is collected
// as it no longer can be referenced.Tuomas Rinta

Similar Messages

  • Garbage Collection facilitation by assigning variables to null

    Hello,
    I understand Garbage Collection (GC) can be facilitated by assigning variables to null. Is this true for all variables?
    I have a class and there are some member variables which are strings. Also i have some methods with some string variables declared and defined inside the method. I am assigning null to class member variables and also the string variables of the method.
    is it correct to assign null and faciliate GC for the string variables declared and defined inside the method?
    Class A {
    String t1;
    public void test() {
    String t2 = "test";
    // processing
    t2 = null; // Will this facilitate GC?
    public static void main (String[] args) {
    test();
    A obj = new A();
    A.t1 = "testclass";
    A.t1 = null; // Will this faciliate GC?
    Thanks, Aravinth

    In fact, the java heap is divided into a space for new allocations, and two survivor spaces. The algorithm for when collection occurs in each space is complex, and tuning GC is an interesting topic all its own. Follow the best advice you have been given ... chillax. The JVM has GC under control.
    <think/> Of course, having said this, you can have memory leaks if you create references that never go out of scope, even if they are no longer used. Avoid this.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Garbage collection Java Virtual Machine : Hewlett-Packard Hotspot release 1.3.1.01

    "Hi,
    I try and understand the mechanism of garbage collection of the Java Virtual Machine : Hewlett-Packard Hotspot release 1.3.1.01.
    There is description of this mechanism in the pdf file : "memory management and garbage collection" available at the paragraph "Java performance tuning tutorial" at the page :
    http://h21007.www2.hp.com/dspp/tech/tech_TechDocumentDetailPage_IDX/1,1701,1607,00.html
    Regarding my question :
    Below is an extract of the log file of garbage collections. This extract has 2 consecutive garbage collections.
    (each begins with "<GC:").
    <GC: 1 387875.630047 554 1258496 1 161087488 0 161087488 20119552 0 20119552
    334758064 238778016 335544320
    46294096 46294096 46399488 5.319209 >
    <GC: 5 387926.615209 555 1258496 1 161087488 0 161087488 0 0 20119552
    240036512 242217264 335544320
    46317184 46317184 46399488 5.206192 >
    There are 2 "full garbage collections", one of reason "1" and one of reason "5".
    For the first one "Old generation After " =238778016
    For the second "Old generation After " =238778016
    Thus, "Old generation Before garbage collection" of the second is higher than "Old generation After garbage collection". Why?
    I expected all objects to be allocated in the "Eden" space. And therefore I did not expect to s

    I agree but my current Hp support is not very good on JVM issues.
    Rob Woollen <[email protected]> wrote:
    You'd probably be better off asking this question to HP.
    -- Rob
    Martial wrote:
    The object of this mail is the Hewlett-Packard 1.3.1.01 Hotspot JavaVirtual Machine
    release and its garbage collection mechanism.
    I am interested in the "-Xverbosegc" option for garbage collectionmonitoring.
    I have been through the online document :
    http://www.hp.com/products1/unix/java/infolibrary/prog_guide/java1_3/hotspot.html#-Xverbosegc
    I would like to find out more about the garbage collection mechanismand need
    further information to understand the result of the log file generatedwith the
    "-Xverbosegc"
    For example here is an extract of a garbage collection log file generatedwith
    Hewlett-Packard Hotspot Java Virtual Machine. Release 1.3.1.01.
    These are 2 consecutive rows of the files :
    <GC: 5 385565.750251 543 48 1 161087488 0 161087488 0 0 20119552 264184480255179792
    335544320 46118384 46118384 46137344 5.514721 >
    <GC: 1 385876.530728 544 1258496 1 161087488 0 161087488 20119552 020119552 334969696
    255530640 335544320 46121664 46106304 46137344 6.768760 >
    We have 2 full garbage collections, one of Reason 5 and the next oneof Reason
    1.
    What happened between these 2 garbage collections as we got : "Oldgeneration
    After" of row 2 is higher than "Old generation Before" of row 1? Iexpected Objects
    to be initially allocated in eden and so we could not get "old generation2modified
    between the end of one garbage collection and before the next one.
    Could you please clarify this issue and/or give more information aboutgarbage
    collection mechanisms with the Hewlett-Packard Hotspot Java VirtualMachine. Release
    1.3.1.01.

  • DocumentBuilderFactory.newInstance() only works after garbage collection

    Hi all,
    I am stucked with a strange behaviour of "DocumentBuilderFactory.newInstance()".
    I use the DocumentBuilderFactory in an applet
    to parse an xml-file.
    My applet starts ok until the line 5 (newInstance) gets called.
    At this time it seems that nothing happens anymore and that
    the applet fails to continue.
    But when I open up the java console and hit
    g (garbage collection) the xml parsing continues immediatly
    and my applet completes loading and runs ok.
    1 private void genarteDomNodes() {
    2 try {
    3 // ---- Parse XML file ----
    4 DocumentBuilderFactory factory =
    5 DocumentBuilderFactory.newInstance();
    6 DocumentBuilder builder =
    7 factory.newDocumentBuilder();
    8 Document document = builder.parse(
    9 new URL(this.myModules_xml).openStream() );
    10 // ---- Get list of nodes to given element tag name ----
    11 NodeList ndList =
    12 document.getElementsByTagName("meter-group");
    13 printNodesFromList( ndList );
    14 } catch( SAXParseException spe ) {
    15 ...
    16 } catch( SAXException sxe ) {
    17 ...
    18 } catch( ParserConfigurationException pce ) {
    19 ...
    20 } catch( IOException ioe ) {
    21 ...
    22 }
    23 }

    I am still stucked with this problem. But I found out how to enable JAXP debug output for applets (setting system properties isn't allowed for applets).
    This puts somemore output to the java console. It might help someone to understand my problem. I also printed some debug messages to track down my problem.
    Following is the console output of my applet:
    URL of XML to parse = "http://10.110.132.195/c8000-modules.xml"
    entering "genarteDomNodes"
    just before calling "DocumentBuilderFactory.newInstance()"
    JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory
    !!! at this time the applet "hangs" and nothing happens;
    until I hit the "g" button. Then applet continues immediatly and prints:
    JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
    JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl using ClassLoader: sun.plugin.security.PluginClassLoader@32060c
    After invoking the garbage collector the applet continue to parse the xml file and runs as expected.
    Can someone help me please.

  • Resources garbage collection, can be done this way ?

    The problem
    I would like to apply the same concept of garbage collection to system resources like Files, Database resulsets, Sockets etc...
    The reasons
    I am tired of looking around desperately into a program to identify memory leak that are instead resources leak. And this is getting worse while program size grows.
    How it could be done
    -- Let's have a resource manager class from where I get the wanted resource (I will write this class, fairly easy).
    -- Let's say that this manager class is a thread that runs in a scheduled way and this is decided by me (this is also something anybody can write).
    -- Let's say that when it runs it checks for all resources (objects) that it has given to the rest of the program if the reference count greater than one. If it is > 1 then the resource is still in use, else it can be deallocated (that just means calling close and then leaving the object to garbage collect).
    What I need
    A way to know in a reliable way how many objects are having a reference to a given object. Is there such a way ?
    Evangelism on..
    Please, if anybody at Sun is reading this, this IS a TOP issue. If you look around you will see more and more issues of leaking, they are resources leakage because it is NOT easy to ALWAYS close a resource.
    Someone may say "use the finally" of yes, try to think to use the finally to deallocate memory and you will immediatly understand the mess that will come.
    Garbage collection is a good idea let's have a way to apply it to resources.
    Thanks

    I have no problem, as it is, in making applications
    reliable.As usual the problems come in deciding how to do
    things.Yes.
    >
    Which is why mine don't. Since resources are usedin
    so few places it is easy to manage their usage.We are not talking on who has the biggest gun...Eh?
    >
    The sad thing is that the problem most of
    the time is NOT yours but of somebody else thatforgot
    to clean up after himself...Sounds more like a management problem than a code
    problem. If you are allowing programmers in your
    organization to write code which leaks resources oris
    so poorly designed that other users can notproperly
    dispose of resources then it is a managementproblem.
    Ok, let's drop it here.
    I hope you will never have to deal with code other
    that yours or written by your coworkers.
    I always deal with other people's code. And they with mine. Although I occassionally work on one person projects - perhaps 2 weeks every three years. the rest of my career has always involved multi-person projects and often (if not always) involve third party libraries.
    So I believe I have dealt with others code.
    in the applications that I work on.)There is java.lang.ref in a way it seems to dowhat
    I
    want....
    I don't believe so. Finalize() more closely fitswhat
    you might want to do. java.lang.ref is used in
    building caches of disposable entities. And it is
    most definitely only useable with memory.I have a funny sensation that whatever I write you say
    no...Not necessarily.
    Thanks for your help, if anybody wants to add
    something useful to
    what I am tryng to do they are welcome.I am trying to be useful.
    I am trying to point out that you are looking for a new feature in the language that just isn't very useful to most people. I was trying to point out why it wasn't useful to most people.
    Perhaps this might help.
    Look at it this way, go look at the bug database and look at the top vote getters for things that people want fixed. Then you might trying looking through the RFEs for something like you want. If it isn't there then add it. If enough people actually want it then it will be voted for. With enough votes it would have a better chance of being added.
    Of course that presumes it could be done in the first place. Based on my memory of past topics in the forums on reference counting I suspect that the answer is no. But you might want to read those.

  • A question about JNI references, persistence, and garbage collection

    I am writing a library to allow our Java developers to access C functions in our product. I want people on the Java side to be able to instantiate instances of a new object (on the C side), say "Map", and I want those objects to persist until they are destroyed on the Java side. I am thinking of creating a wrapper for the native methods, which stores an identifier for the instance of an object. When the C object is accessed, the identifier is passed to the C code which looks up the correct object in a table. I understand that if I use the NewGlobalReference call in JNI I can get objects to persist.
    When it is time for garbage collection I plan to override finalize() to call the C code and tell it to remove the Global Reference.
    Here's what I have in mind
    public class JMap() {
         private static int id;
         static {
              System.loadLibrary("libMap");
              /*Call C method which instantiates object and creates a GlobalReference
              which is stored in table. Returns ID for reference*/
              id = _init();
         public void setSize(int x, int y) {
              _setSize(id, x, y);
         public void finalize() {
              _finalize(id);
              super.finalize();
         private native int _init();
         /*calls DeleteGlobalReference for the reference matching this id*/
         private native void _finalize(int id);
         private native void _setSize(int id, int x, int y);
    }Is this the right thing to do? Have I understood the way JNI works with regard to GlobalReferences?
    Any comments, tips or pointers would be appreciated :)

    This probably should have been posted in the Native Methods sub-forum, I have reposted there:
    http://forum.java.sun.com/thread.jspa?threadID=657667
    Mods please delete this post :)

  • Aggressive Heap and garbage collection in 1.4.2

    We are trying to monitor the memory usage and the garbage collection for one of our application servers. We are using JDK 1.4.2 b-19 on solaris 8. The machine has 4 900 Mhz CPUs and 8GB ram. The VM options are:
    -server -XX:+AggressiveHeap -Xms3512m -Xmx3512m -verbose:gc
    -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+PrintGCTimestamps.The first full GC kicks in when the old generation gets full which is understandable. After that however, the full gc kicks in much more frequently and even the gc pauses are fairly long. Below are some of the output from gc.
    For the first GC the old generation was almost full.
    PSYoungGen total 1379840K, used 1324288K
    PSOldGen total 2123648K, used 2103378K
    For the second GC, young generation is 25MB out of 1300MB and old generation is 1100MB out of 2100MB. Still the full gc kicked in and took 23 secs.
    PSYoungGen total 1356608K, used 25856K
    PSOldGen total 2123648K, used 1169626K
    I understand that the adaptive sizing is kicking in (by default) but it should make life better not worse. If the full GC could wait till the old generation gets full again, I will get lesser number of full gc pauses and few and far in between.
    Any idea as to what I can do?
    thanks
    vinay
    {Heap before GC invocations=108:
    Heap
    PSYoungGen      total 1379840K, used 1324288K [0x1a000000, 0x73e20000, 0x73e20000)
      eden space 228096K, 100% used [0x1a000000,0x27ec0000,0x27ec0000)
      from space 1151744K, 95% used [0x27ec0000,0x6ad40000,0x6e380000)
      to   space 92800K, 0% used [0x6e380000,0x6e380000,0x73e20000)
    PSOldGen        total 2123648K, used 2103378K [0x73e20000, 0xf5800000, 0xf5800000)
      object space 2123648K, 99% used [0x73e20000,0xf4434af8,0xf5800000)
    PSPermGen       total 31872K, used 31768K [0xf5800000, 0xf7720000, 0xf9800000)
      object space 31872K, 99% used [0xf5800000,0xf7706200,0xf7720000)
    2842.142: [Full GC 3427666K->1083494K(3503488K), 26.6495189 secs]
    Heap after GC invocations=108:
    Heap
    PSYoungGen      total 1379840K, used 0K [0x1a000000, 0x73e20000, 0x73e20000)
      eden space 1287040K, 0% used [0x1a000000,0x1a000000,0x688e0000)
      from space 92800K, 0% used [0x6e380000,0x6e380000,0x73e20000)
      to   space 92800K, 0% used [0x688e0000,0x688e0000,0x6e380000)
    PSOldGen        total 2123648K, used 1083494K [0x73e20000, 0xf5800000, 0xf5800000)
      object space 2123648K, 51% used [0x73e20000,0xb6039978,0xf5800000)
    PSPermGen       total 63744K, used 31768K [0xf5800000, 0xf9640000, 0xf9800000)
      object space 63744K, 49% used [0xf5800000,0xf7706200,0xf9640000)
    {Heap before GC invocations=114:
    Heap
    PSYoungGen      total 1356608K, used 25856K [0x1a000000, 0x73e20000, 0x73e20000)
      eden space 1240576K, 0% used [0x1a000000,0x1a000000,0x65b80000)
      from space 116032K, 22% used [0x6ccd0000,0x6e610000,0x73e20000)
      to   space 116032K, 0% used [0x65b80000,0x65b80000,0x6ccd0000)
    PSOldGen        total 2123648K, used 1169626K [0x73e20000, 0xf5800000, 0xf5800000)
      object space 2123648K, 55% used [0x73e20000,0xbb456858,0xf5800000)
    PSPermGen       total 65536K, used 31773K [0xf5800000, 0xf9800000, 0xf9800000)
      object space 65536K, 48% used [0xf5800000,0xf7707578,0xf9800000)
    3153.022: [Full GC 1195482K->1149586K(3482688K), 23.3155823 secs]
    Heap after GC invocations=114:
    Heap
    PSYoungGen      total 1359040K, used 0K [0x1a000000, 0x73e20000, 0x73e20000)
      eden space 1245440K, 0% used [0x1a000000,0x1a000000,0x66040000)
      from space 113600K, 0% used [0x6cf30000,0x6cf30000,0x73e20000)
      to   space 113600K, 0% used [0x66040000,0x66040000,0x6cf30000)
    PSOldGen        total 2123648K, used 1149586K [0x73e20000, 0xf5800000, 0xf5800000)
      object space 2123648K, 54% used [0x73e20000,0xba0c4830,0xf5800000)
    PSPermGen       total 65536K, used 31773K [0xf5800000, 0xf9800000, 0xf9800000)
      object space 65536K, 48% used [0xf5800000,0xf7707578,0xf9800000)
    }

    We also had similar issues...If you are using RMI it might cause Full GC. This is the snippet from http://java.sun.com/docs/hotspot/gc/
    <snip>
    Garbage can't be collected in these distributed applications without occasional local collection, so RMI forces periodic full collection. The frequency of these collections can be controlled with properties. For example,
    java -Dsun.rmi.dgc.client.gcInterval=3600000
    -Dsun.rmi.dgc.server.gcInterval=3600000
    </snip>
    By default this is 1 min. So try changing this and try it out.

  • Revisited: static synchronized vs garbage collected

    A forum thread (http://forum.java.sun.com/thread.jsp?forum=4&thread=466049&start=0&range=15&hilite=false&q=) from last November addressed issues involving the relative merits of using static methods vs. the garbage collection issues that might attend conventional object instantiation. The discussion extended into use of synchronized code and threading, but from it, I've failed to gather an understanding of a few subtleties.
    I understand the dangers of using class-level variables within servlets, however, I'm still puzzled as to the thread-safety of 1) static class-level variables in servlets, and 2) using static methods residing in "utility" type classes from within servlets. I also realize these matters are not confined to servlets, but extend to any multi-threaded environment. I'm particularly unclear as to when, or if, the methods of the static class-level variable/objects need to be synchronized; with the same question applied to the static methods of the "utility" classes.
    From the forum thread mentioned above, it would seem that synchronization is NOT needed since parameters and method-level variables are specific to the executing threads. I believe this essentially means that static methods should not rely on the state of their containing class (under most applications). In a situation where the method's code is not trivial or "lightweight" (e.g. database access), would not the overhead associated with the swapping in and out of thread-specific variables lead to performance issues?
    My particular situation involves an ecommerce site running websphere. We are experiencing sporadic episodes where the VM starts running low on memory, will occasionally recover but will also occasionally crash. In pursuing this wild goose, my first suspicion was a thread deadlocking problem since it's not reliably reproducible. Most of the log entries point to a static method in one of these "utility" classes, yet I can find no non-static class level variables either in the utility or the servlet. However, none of these static methods are synchronized.
    Sorry for the ramble, and thanks for any help.
    Regards,
    Joe

    Because static variables, by their very nature, are only instantiated once, you MUST syncronise them to be safe in a multi threaded environment, otherwise you will get unexpected results.
    As you rightly mention, servlets also optimise the instantiation of class level variables and effectively make them static as well, so they are shared amongst all instaniations of a servlet.

  • At which line , Object is Garbage collected.

    1. public class GC {
    2.  private Object o;
    3.   private void doSomethingElse(Object obj) { o = obj; }
    4.   public void doSomething() {
    5.  Object o = new Object();
    6.  doSomethingElse(o);
    7.   o = new Object();
    8.   doSomethingElse(null);
    9  o = null; 10. }
    } When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection?
    A. Line 5
    B. Line 6
    C. Line 7
    D. Line 8
    E. Line 9
    F. Line 10
    I think it should be line 9.but answer is 8....
    How is it line 8?
    Edited by: user12203354 on Nov 4, 2012 9:38 PM

    You've already ask a similar question in your other thread where I ask you to answer some questions.
    Question on Garbage collection
    You may want to work on one question like this at a time until you understand the difference between an object and a variable that references an object.

  • A possibility to prevent garbage collection?

    inspired by the topic "Why can we force Garbage Collection" i asked myself if it is possible to explicitly prevent the automatic garbage collection of the VM.
    regards
    ~elchaschab

    hmm.. you didn't understand my question.
    it's not about prevent the VM from freeing unused
    objects, it's about preventing it doing it at the
    wrong moment (e.g. a highly interactive part of code).No. But Sun's current GC works in smallish increments to reduce the impact. In extremis, you'd have to look at the realtime java project.
    As for preventing objects from being reclaimed, even storing a reference to the object isn't sufficient. You have to provide code that convinces the optimiser that the object will actually be used again in a way that the optimise can't conclude is functionally null. Putting it in a list that will be traversed by an on-exit handler, which in turn will pass it to a native method would do it. Not sure whether anything else is sufficient.
    Now throw in Reference objects and finalizers, and things really get exciting :(
    Sylvia.

  • Garbage Collection Help

    My understanding of garbage collection is that resources utilized by an object are released when no reference to the object exist. I accomplish this by ensuring that all references to an unneeded object are null.
    My question/concern is this. If I have an object "A" that has references to other objects "B", "C", and "D", will the all objects be elibible for garbage collection when the reference to object A is set to null? I suspect that objects "B", "C", and "D" persist until the virtual machine is terminated.
    Is the practice of creating a method within a class to make all inner objects eligible for garbage collection prior to setting the object reference to null. (i.e. set objects "B", "C", and "D" to null prior to setting "A" to null).
    Maybe I am just paranoid??

    My understanding of garbage collection is that
    resources utilized by an object are released when no
    reference to the object existThat's not correct. Objects can have references that point to them and still be garbage collected. For example:
    Vector v = new Vector();
    v.add(new Object());
    v = null;
    After this code is executed, the Vector object has a reference to the "new Object()" created. However, the Vector itself is not reachable, and therefore the "new Object()" is unreachable, and therefore both are collected.
    The garbage collector collects unreachable objects.
    I accomplish this by
    ensuring that all references to an unneeded object are
    null.It is quite rare that setting a reference to null is needed to make an object unreachable. Don't do it -- it makes your code slower, less readable, and harder to modify.
    My question/concern is this. If I have an object "A"
    that has references to other objects "B", "C", and
    "D", will the all objects be elibible for garbage
    collection when the reference to object A is set to
    null? I suspect that objects "B", "C", and "D"
    persist until the virtual machine is terminated.Yes -- all will be collected when A becomes unreachable. As noted earlier, you shouldn't need to set any reference to null for this to happen.
    Is the practice of creating a method within a class to
    make all inner objects eligible for garbage collection
    prior to setting the object reference to null. (i.e.
    set objects "B", "C", and "D" to null prior to setting
    "A" to null).
    Maybe I am just paranoid??Yes. Just let the garbage collector do its job and collect unreachable objects. You should almost never need to write any code to "help" the garbage collector.

  • Garbage Collection is not destruction?

    Garbage Collection is not destruction?
    This is one of the point from Thinking In Java 4th edition Chapter 5.Can someone please explain me this point.Does that mean that Garbage Collection does not destroy objects.If yes,then what is it role?
    And also please explain something about finalize().I tried to read it but couldn't understand.
    Thanks for your help.

    personal opinion really. I do call garbage collection destruction, the memory occupied by the objects in question is reclaimed. It is a discussion point however as it is from the perspective of the JVM; from the perspective of the application the object is "destroyed" as soon as you cut all references to it.
    What to explain about finalize()? It is invoked right before an object is garbage collected, possibly allowing you to do some last minute clean up jobs, like letting go of resource handles and such. The general rule about it is that in 99.99% of the cases you need not touch it. If you have a design where you need it, it is best to assume the design is wrong and you need to change it. For example in the case of closing resources, it is better to have a close() method that needs to be invoked manually.
    If you do use it, you better be really sure that you know what you are doing as you may cause performance problems or even hard to trace unexpected behavior.

  • Garbage Collection Mysteries

    I have an application that consumes huge amounts of heap at time, on the order of 100 MB. I added some code to call System.gc() when I dispose of the window that is responsible for making such huge allocations. My understanding is that System.gc() is supposed to do a full garbage collection, but when I am watching the heap in the Eclipse profiler it does not seem to reclaim very much. In almost ever case, when I click the garbage collect button in the profiler it seems to do a much better job of collecting a lot.
    What is the difference between explicitly invoking the garbage collector from the Eclipse profiler and calling System.gc() in code when the window is disposed?
    How can I get my code to automatically make the garbage collector work so well?
    Cheers, Eric

    jschell wrote:
    Might note however that calling gc() is unlikely to do anything to make your application better.Ain't it the truth. In fact it may even make it worse. Sane applications should not need manual GC calls - the runtime does what it is supposed to do itself and if it doesn't you should investigate and fix the problem that is preventing it from doing what it is supposed to do. First making sure that there is in fact an issue of course, perhaps the app simply needs 100mb of heap space at some point in the lifetime of your application. It isn't exactly a HUGE amount of memory, especially in a Java or .NET VM environment.

  • [JS] $.gc() - garbage collection, etc.

    Hello everyone, bear with me for all the questions.
    $.gc() - Initiates garbage collection in the JavaScript engine.
    Ok, maybe I have a fundamental misunderstanding of things here, but I am wanting to understand a few things:
    I was wanting to know if $.gc() can be called and executed outside of running scripts directly from with in the ESTK toolkit? The reason I ask is that its listed in the tools guide but not the javascript reference. I know things like $.writeln() only seem to apply/display from with in the ETSK toolkit, is it still executed otherwise silently when it appears in a script, $.sleep() works fine when used. So what about $.gc(), can I call it and have it execute from within my scripts outside of running them via the ETSK toolkit?
    Is there a way to test if its in fact being called, executed, working? I have seen people talking about calling it twice and doubling up, $.gc() $.gc(), thus does it even work, or is it just a fabled mythical thing.
    Do codes when executed outside the ETSK toolkit get funneled through the same javascript runtime engine that they do when executing from with in the ETSK toolkit? Is it all the same and the ETSK toolkit is just Adobe's GUI for writing, debugging, targeting different api's for the various programs with in the same underlying engine?
    Does ScriptBay tap into and use this same underlying framework? Does it do anything inherently on its own for garbage collection? (since it can run scripts sequentially, repetitively, etc..)
    Does anyone know what embedded javascript runtime/engine Adobe is using? This would be beneficial for any specific optimizations that can be considered or kept in mind in general based on the specific JS engine thats implemented when coding.
    So aside from the mythical $.gc() , what other best practices for garbage collection should be considered? I know people suggest wrapping your codes in a function, talk of #targetengine (saw in InDesign forum), etc… are there any other common do's and dont's, best practices pertaining specifically to illustrator? Granted I can find a lot about Javascript in general across the web but what about how it applies to Illustrator, its api, limitations, performance, garbage collection, etc..
    Thanks. ;-)
    Again, sorry for all the questions, I am still trying to continue to wrap my head around different aspects of this whole thing. Thanks everyone for any feedback your able to offer and provide. Many thanks in advance, its greatly appreciated. Thanks again.

    I can't answer or help with all of those… Here are my thoughts… The dollar object properties and functions can be called on by script from AI, BR, ID & PS… They are NOT restricted for use in the ESTK tookit. Yeah $.writeln() would write to the console if the toolkit were open if NOT then it will probably launch it… So remove or comment these out. As you can see from $.sleep() it does what it says when run in the host app. #targetengine is exclusive to ID this app can have multi-instances ( or something like that ). The other apps have 1 engine and thats it. AI's engine is persistent so wrapping your code is good practice I do this for BR & PS too… See…
    http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/illustrator/scrip ting/readme_cs5.txt
    As for garbage collection I've never fould the need to use it… I see some like to set variables back to null but again I don't do this…
    You would have been better off asking this question in the ID scripting forum… There are several people there who are better qualified to answer some of this…

  • Runtime.getRuntime().exec() and Garbage Collection

    I am programming a piece of software in both Java and C that has some strict real time requirements. Garbage collection, which pauses all threads in Java, sometimes causes loss of incoming data. In order to get around this, I am thinking to start another process using Runtime.getRuntime().exec("c_program") and using interprocess controls (in a UNIX environment) to retrieve data from the new process.
    My only worry is that the Process created by the above call would be a child process of whatever JVM thread created it, (as far as I understand, the JVM implementation in Unix uses multiple processes) and would also be paused when garbage collection occurs. Does anyone know the implementation of the exec functionality and the JVM well enough to say that this will or will not happen?
    Thanks in advance,
    Benjamin

    You're going to create a whole new process? I don't
    know what a "child process" means, but Runtime.exec()
    gets the operating system to produce an entirely new
    process, outside the JVM. However if it produces
    output on stdout or stderr, you're going to have
    threads in your JVM that read that output, otherwise
    that process will hang.
    Why is your idea better than just calling the C
    program via JNI?Thank you both for your replies. My plan was to create a whole new process, yes. In UNIX, a process C is created by another process P using fork() or the exec() family. Process P is then the parent of process C, and process C is the child of Process P. P has an amount of control over C since it can send various signals to pause, kill, etc to C.
    My concern was that the JVM implementation would use these signals to implement the pause of all threads before garbage collecting. If it did, it may also pause the Process that it spawned from the Runtime.exec() call. Pausing my C program in this manner would cause the loss of data I was trying to avoid in the first place.
    My plan for the new process was not to produce anything on stdout or stderr, but to pass data back to the JVM using ipc (interprocess communication) resources of UNIX. I would have to use the JNI to access these resources.
    The whole reason for wanting to do this is to avoid the pause during garbage collection that all Java Threads experience. If I were just to call the C program through the JNI with a normal Java Thread as I think you were suggesting, this Java Thread would still be paused during garbage collection.
    To the second reply about RTSJ, I had heard about this but couldn't find info about it! Thanks for the link. I'm checking it out at the moment. The java runtime must be considerably different for the specifications I see that they guarantee.
    Again, thanks for the replies,
    Benjamin

Maybe you are looking for

  • HT1435 My Ipod Shuffle no longer seen by PC or Itunes

    After the latest Itunes update, my IPOD Shuffle is no longer seen by PC or Itunes.  I cannot change or modifly playlists. Ipod still takes a charge.

  • This mobile is already registered and no extend op...

    Hello, yesterday I tried to extend my Ovi Music unlimited. There was no extend option visible to me, even though my subscription to OVI Music unlimited had ended one week prior. So I clicked on the respective link on the home screen of Nokia OVI Play

  • Update and Overwrite Option in Import Master Data Package

    Hello, In the package Import Master Data there is the option "Select method for loading master data". I don't understand very well what is the difference between the "Update" and "Overwrite" option. I have tested both and I get the same result. Can s

  • How to Call vector in Jsp from My factory class

    Hi all, I am new to singleton,I written a singleton class,One more all so name is getAllFactory(),this method returns vector which i am going to adding my data to xxDto finally i am adding this xxDto to my Vector in the singleton class. I need help h

  • Viewing most used keyword tags in Lightroom

    My department is making an overhaul on our keyword usage and we want to figure out which keyword tags have been used the most and which ones are rare and uncommon. Is there a way to export a spreadsheet and/or view the keyword statistics? We are usin