Jump to content

[Snippet] Throwable decorator with system and memory information


Recommended Posts

Posted (edited)

Useful to spot nasty jvm, system and memory-specific issues (outdated java, exotic operating system, incorrect memory allocation, etc...).

I personally only decorate critical / fatal exceptions with this. There's no point in casually wrapping it around everything (unless you really love verbose stack traces).

 

Example:

public class Test {
	public static void main(String[] args) {
		try {
			System.out.println(1/0);
		} catch (Exception e) {
			ThrowableDecorator.withSystemAndMemoryInformation(e).printStackTrace();
		}
	}
} 

Snippet:

public final class ThrowableDecorator {

	private ThrowableDecorator() {
		
	}
	
	public static final Throwable withSystemAndMemoryInformation(Throwable throwable) {
		StringBuilder sb = new StringBuilder();
		//System
		sb.append("\n\tSystem information:");
		System.getProperties().entrySet().forEach(entry -> sb.append("\n\t\t" + entry.getKey().toString() + ": " + entry.getValue().toString()));
		//Memory
		sb.append("\n\tMemory information:");
		sb.append("\n\t\tAvailable processors (cores): " + Runtime.getRuntime().availableProcessors());
		sb.append("\n\t\tFree memory (bytes): " + Runtime.getRuntime().freeMemory());
		sb.append("\n\t\tMaximum memory (bytes): " + Runtime.getRuntime().maxMemory());
		sb.append("\n\t\tTotal memory (bytes): " + Runtime.getRuntime().totalMemory());
		return new Throwable(sb.toString(), throwable);
	}
	
} 

Example trace:

java.lang.Throwable: 
	System information:
		java.runtime.name: Java(TM) SE Runtime Environment
		sun.boot.library.path: C:\Program Files\Java\jre1.8.0_77\bin
		java.vm.version: 25.77-b03
		java.vm.vendor: Oracle Corporation
		java.vendor.url: http://java.oracle.com/
		path.separator: ;
		java.vm.name: Java HotSpot(TM) 64-Bit Server VM
		file.encoding.pkg: sun.io
		user.country: GB
		user.script: 
		sun.java.launcher: SUN_STANDARD
		sun.os.patch.level: 
		java.vm.specification.name: Java Virtual Machine Specification
		user.dir: *****
		java.runtime.version: 1.8.0_77-b03
		java.awt.graphicsenv: sun.awt.Win32GraphicsEnvironment
		java.endorsed.dirs: C:\Program Files\Java\jre1.8.0_77\lib\endorsed
		os.arch: amd64
		java.io.tmpdir: *****
		line.separator: 
		java.vm.specification.vendor: Oracle Corporation
		user.variant: 
		os.name: Windows 10
		sun.jnu.encoding: ******
		java.library.path: *****
		java.specification.name: Java Platform API Specification
		java.class.version: 52.0
		sun.management.compiler: HotSpot 64-Bit Tiered Compilers
		os.version: 10.0
		user.home:*****
		user.timezone: 
		java.awt.printerjob: sun.awt.windows.WPrinterJob
		file.encoding: Cp1252
		java.specification.version: 1.8
		java.class.path: *****
		user.name: bjorn
		java.vm.specification.version: 1.8
		sun.java.command: org.botre.exception.Test
		java.home: C:\Program Files\Java\jre1.8.0_77
		sun.arch.data.model: 64
		user.language: en
		java.specification.vendor: Oracle Corporation
		awt.toolkit: sun.awt.windows.WToolkit
		java.vm.info: mixed mode
		java.version: 1.8.0_77
		java.ext.dirs: *****
		java.vendor: Oracle Corporation
		file.separator: \
		java.vendor.url.bug: http://bugreport.sun.com/bugreport/
		sun.io.unicode.encoding: UnicodeLittle
		sun.cpu.endian: little
		sun.desktop: windows
		sun.cpu.isalist: amd64
	Memory information:
		Available processors (cores): 4
		Free memory (bytes): 253398816
		Maximum memory (bytes): 3795845120
		Total memory (bytes): 257425408
	at org.botre.exception.ThrowableDecorator.withSystemAndMemoryInformation(ThrowableDecorator.java:21)
	at org.botre.exception.Test.main(Test.java:9)
Caused by: java.lang.ArithmeticException: / by zero
	at org.botre.exception.Test.main(Test.java:7)

Edited by Botre
  • Like 4
Posted

Luckily enough I rarely have to rely on this information biggrin.png

When it's usefull it tends to be very usefull and often ends up saving quite a lot of time though tongue.png

Makes sense, the only issue I can see is that you need a single try catch to decorate (A main one) or you need the foresight to include this decorator in the offending method! 

 

Ah, the struggles of programming :D

  • Like 1
Posted

Makes sense, the only issue I can see is that you need a single try catch to decorate (A main one) or you need the foresight to include this decorator in the offending method! 

 

Ah, the struggles of programming biggrin.png

 

Indeed x)

You can decorate strategically or just go all-in, I prefer the former approach so I don't get hugely verbose traces every time I forget a simple null check :p

 

I personally use them mostly to decorate IOException and its sub-classes and Swing-related exceptions.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...