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
Solved

Garbage collector thread priority..

Posted on 1999-01-21
11
773 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

856 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