Solved

Garbage collector thread priority..

Posted on 1999-01-21
11
770 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

920 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

15 Experts available now in Live!

Get 1:1 Help Now