public class ResourceTag extends Object
This tag is attached upon resource requests and detached upon resource release. If resources are supposed to be released but actually not released, a caution is generated.
For example, if an object is reclaimed by a virtual machine (VM) but its resources are not released, the corresponding caution is generated.
Sample code for using ResourceTag:
public class XxxResource {
private final ResourceTag resTag = ResourceTag.get();
private final String TAG_MSG = "XxxResource opened by the get method, but not closed by the close method";
public void get() {
...;
resTag.markInUse(TAG_MSG);
}
public void close() {
resTag.release();
...;
}
protected void finalize() {
resTag.warnIfNeed();
...;
}
}
Rule#CHECK_VM_RELEASE_MISS| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static ResourceTag |
get() |
Obtains a new
ResourceTag object. |
void |
markInUse(String resourceTag) |
Marks a resource tag as in use so that it is attached to the paired resource.
|
void |
release() |
Releases a resource tag so that it is detached from the paired resource.
|
void |
warnIfNeed() |
Determines whether to generate a caution based on the resource tag state.
|
public static ResourceTag get()
ResourceTag object.
To use the resource tag mechanism for resources, you need to call this method and attach the obtained resource
tag to resources via markInUse(java.lang.String).
ResourceTag object.public void markInUse(String resourceTag)
resourceTag - Indicates the custom resource description, which is used to notify you of details about the
resource not correctly released. The following format is recommended: Resources are requested
by the xxx class name#open() method and need to be closed by the xxx class name#close() methodpublic void release()
If the resource tag is released, resources attached to the tag will also be closed or released.
public void warnIfNeed()
If the resource tag is in use and the detection rule Rule.VM_CHECK_RESOUCE_LEAK was added for your
application, calling this method will generate a caution unless you have called the release() method.
Rule#CHECK_VM_RELEASE_MISS