Solved

Garbage collector thread priority..

Posted on 1999-01-21
11
776 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 131
jdbc error in jsp application 20 74
hibernate insert example 13 40
maven disable workspace resolution 1 44
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

697 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