Solved

Garbage collector thread priority..

Posted on 1999-01-21
11
771 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_
ID: 1234422
there is no stadard way to achive this.

regards
  heyhey
0
 
LVL 1

Expert Comment

by:velev
ID: 1234423
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
ID: 1234424
Perhaps it would help if you could explain why you want to do this. Maybe there is another solution to your problem.

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Accepted Solution

by:
kinross earned 50 total points
ID: 1234425
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
ID: 1234426
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1234427
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
ID: 1234428
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
ID: 1234429
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_
ID: 1234430
>> 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
ID: 1234431
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
ID: 1234432

 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

813 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

14 Experts available now in Live!

Get 1:1 Help Now