Solved

Garbage collector thread priority..

Posted on 1999-01-21
11
769 Views
Last Modified: 2013-12-22

 Hello friend,

    In Java , a low priority thread will run the garbage
    collector. But now i want to increase the priority
    of that thread. How can i do that ?

    I used the following code to invoke the gc ..

    Runtime rt = Runtime.getRuntime();
    rt.gc();

    Since it's a low priority thread , the other threads
    are blocking it to run...

    Is there anyother way to achieve this ?

 Thanks in Advance,

 Sankar S.
 e-mail : usubsa01@shafika.vetri.com
0
Comment
Question by:sankars98
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
there is no stadard way to achive this.

regards
  heyhey
0
 
LVL 1

Expert Comment

by:velev
Comment Utility
heyhey is right
JVM decide when to rise the priority of the thread in which
gc is running (  when not enough memory for example)
look in JDK1.2 java.lang.ref.* if you need more flexible
work with object references


0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
Perhaps it would help if you could explain why you want to do this. Maybe there is another solution to your problem.

0
 
LVL 2

Accepted Solution

by:
kinross earned 50 total points
Comment Utility
When a thread is created it inherits the priority of the thread that created it.  The priority of a thread can be adjusted by using public final void setPriority(int newPriority).  Priority is in the range 1..10 with 5 being the norm.  To get the current priority use public final int getPriority().

PS.  A thread doesn't "run" the garbage collector.  The garbage collector is run by the VM and runs whenever java feels like it.
0
 
LVL 2

Expert Comment

by:kinross
Comment Utility
Straight from the JDK API.

public void gc()

     Runs the garbage collector. Calling this method suggests that the Java Virtual Machine expend effort toward recycling unused objects in
     order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual
     Machine has made its best effort to recycle all discarded objects.

     The name gc stands for "garbage collector". The Java Virtual Machine performs this recycling process automatically as needed, in a
     separate thread, even if the gc method is not invoked explicitly.

---->   It's best to just let the GC do its thing.  There really is no reason to call this unless you are really really tight on memory.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
kinross, probably you haven't read the question ...
which is
"Can I change the priority of the Garbage collector thread ?"

Most of us know how to change the priority of a standard Thread, and have read the documentation about System.gc() ... :))
0
 
LVL 2

Expert Comment

by:kinross
Comment Utility
The GC is not a thread but a method of the Runtime thread.  You can changed the priority of the Runtime thread but this will have no effect on the GC.  The thread that calls the GC is a thread of the VM and is out side the scope of the API so you can't change its priority.
0
 
LVL 2

Expert Comment

by:kinross
Comment Utility
The GC is not a thread but a method of the Runtime thread.  You can changed the priority of the Runtime thread but this will have no effect on the GC.  The thread that calls the GC is a thread of the VM and is out side the scope of the API so you can't change its priority.
0
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
>> The thread that calls the GC is a thread of the VM and
>> is out side the scope of the API so you can't change its priority.

yes that is.
you can take a look at th eFIRST comment. :))

>> there is no stadard way to achive this.
0
 
LVL 1

Expert Comment

by:velev
Comment Utility
GC runs in a thread
normally this thread has low priority
JVM rise its priority when needed
you can not set the prioritiy of this thread, because it is in another thread group an you can not access it.

but when JVM says out of memory this mean that JVM has already try to clean unused objects.
problem is with memory leeks( with images for example )




0
 
LVL 1

Author Comment

by:sankars98
Comment Utility

 Thanks guys .. Today i learnt a new things from you guys.
 Thanks for those responses. I like to run the gc periodically
 as i am getting "OutofMemory" error.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now